7,762
社区成员
发帖
与我相关
我的任务
分享
Dim textBytes() As Byte, headBytes(2) As Byte
fn = App.Path & "\test.txt"
If Dir(fn) = "" Then
MsgBox "文件不存在,请先单击【写入】按钮生成“MyUnicode.txt”文件。", vbInformation
Exit Sub
End If
Open fn For Binary As #1
Get #1, , headBytes() '读取文件头。
Close #1
If headBytes(0) = 255 And headBytes(1) = 254 Then '是Unicode编码。十六进制为:FF 、FE。
Open fn For Binary As #1
ReDim textBytes(LOF(1) - 2) '减去文件头占用的2个字节。
Get #1, 3, textBytes() '第三个字节起为文本内容。
Close #1
Text1.Text = textBytes() '在 VB 中字符串是 UniCode 格式,所以Unicode码直接赋值即可显示文本内容。
RichTextBox1.Text = textBytes()
Else
MsgBox "非Unicode编码,不予读入,请单击【写入】按钮。", vbInformation
End If