FSO.ReadAll不读取整个文件内容

时间:2019-06-02 06:10:34

标签: vbscript

我正在制作文件加密器/解密器,但遇到FSO.ReadAll无法读取整个文件内容的问题。

下面是代码:

Sub EncryptFile(file) 
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set readf = FSO.OpenTextFile(file, 1)
    c = readf.ReadAll
    readf.Close()
    Set readf = Nothing
    'MsgBox only shows the first 4 characters of the file. ("ÿØÿà")
    'Does ReadAll stop its reading when the character is unreadable for it?
    'n = MsgBox(c, , "")
    '^^^^^ Result: ÿØÿà
    '...more below, but no longer needed for the question
End Sub

MsgBox结果:

MsgBox n result

文件内容(是,文件为.JPG)

File contents

似乎是什么问题?是因为ReadAll停止阅读是因为编码或其他内容无法读取下一个字符吗?

1 个答案:

答案 0 :(得分:0)

ReadAll并不用于读取二进制文件。

尝试使用读取功能代替。

 Sub EncryptFile(file) 
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Dim objFile: Set objFile = oFSO.GetFile(file)

    Set readf = FSO.OpenAsTextStream()
    c = readf.Read(objFile.Size)
    readf.Close()
    Set readf = Nothing
End Sub

请参阅:Read and write binary file in VBscript

相关问题