FileSystemObject - Reading Unicode Files

前端 未结 5 1800
闹比i
闹比i 2021-01-04 09:52

Classic ASP, VBScript context.

A lot of articles including this Microsoft one, say you cannot use FileSystemObject to read Unicode files.

I

5条回答
  •  太阳男子
    2021-01-04 09:58

    I think MS does not officially state that it supports unicode because:

    1. It does not detect unicode files using the byte-order mark at the start of the file, and
    2. It only supports Little-Endian UTF-16 unicode files (and you need to remove the byte order mark if present).

    Here is some sample code that I have been using successfully (for a few years) to auto-detect and read unicode files with FSO (assuming they are little-endian and contain the BOM):

    'Detect Unicode Files
    Set Stream = FSO.OpenTextFile(ScriptFolderObject.Path & "\" & FileName, 1, False)
    intAsc1Chr = Asc(Stream.Read(1))
    intAsc2Chr = Asc(Stream.Read(1))
    Stream.Close
    If intAsc1Chr = 255 And intAsc2Chr = 254 Then 
        OpenAsUnicode = True
    Else
        OpenAsUnicode = False
    End If
    
    'Get script content
    Set Stream = FSO.OpenTextFile(ScriptFolderObject.Path & "\" & FileName, 1, 0, OpenAsUnicode)
    TextContent = Stream.ReadAll()
    Stream.Close
    

提交回复
热议问题