kellyethridge / VBCorLib

The VBCorLib framework brings many of the powerful .NET classes to VB6.
http://www.kellyethridge.com/vbcorlib/
MIT License
112 stars 28 forks source link

File.ReadAllText is not correct. #101

Open halfsprit opened 3 years ago

halfsprit commented 3 years ago

if file is gbk encoding, and the last char is double bytes, File.ReadAllText can't get correct string. s = File.ReadAllText("e:\aa.js", Encoding.GetEncoding("GBK"))

kellyethridge commented 2 years ago

I found the issue, unfortunately it is not a simple fix. The problem is that the Encoder that is being used relies on the Win32 IsDBCSLeadByteEx method. Turns out that method can only tell you if the byte you are checking is probably a lead byte, which can lead false positives. So dependency on that method has to be removed.

If you still want to read an entire file using the encoder, it can be done using:

    Dim En      As Encoding
    Dim Bytes() As Byte
    Dim s       As String

    Bytes = File.ReadAllBytes("e:\aa.js")
    Set En = Encoding.GetEncoding("GBK")    
    s = En.GetString(Bytes)