查看: 22  |  回复: 0
  VB6 奇怪的LOF,实验结果
楼主
发表于 2025年3月5日 14:54

新建From1(窗体),新建Command1(按钮CommandButton),代码:

Private Sub Command1_Click()
    Dim strFN As String
    strFN = "C:\Windows\notepad.exe"
    
    Open strFN For Input As #1
        MsgBox "Input方式得到文件大小:" & LOF(1)
    Close #1
    
    Dim byteArray() As Byte
    
    Open strFN For Binary As #1
        MsgBox "Binary方式得到文件大小:" & LOF(1)
        Seek #1, 1    '移动到指定位置,文件位置从 1 开始
        Get #1, , byteArray    '读取 N 个字节到字节数组
    Close #1
End Sub

在VB6IDE中运行:

Input方式得到文件大小:202240
Binary方式得到文件大小:0

生成exe运行:

Input方式得到文件大小:202240
Binary方式得到文件大小:202240

而改成不是Windows自带的exe,比如:

strFN = "C:\Windows\py.exe"

在IDE中就能得到正确的大小。

另外,Seek语句可以不读取全部文档,直接定位到指定位置读取,速度超快。

您需要登录后才可以回帖 登录 | 立即注册
【本版规则】请勿发表违反国家法律的内容,否则会被冻结账号和删贴。
用户名: 立即注册
密码:
2020-2025 MaNongKu.com