VB超级模块函数VB读写记事本-防止乱码支持UTF-8和GB2312编码

Private Sub Command1_Click()
Writein “C:\Users\Administrator\Desktop\1.txt”, “文本文内容”
End Sub

Private Sub Form_Load()
Text1 = ReadANSI(“C:\Users\Administrator\Desktop\1.txt”)
Text2 = ReadUTF8(“C:\Users\Administrator\Desktop\1.txt”)
End Sub

'写入TXT文件
Private Function Writein(ByVal txtpath As String, ByVal txt As String)
On Error Resume Next
Open txtpath For Output As #1 '打开XXX路径的XXX文件(双引号里表示文件位置和文件名)
Print #1, txt '写入Text1的Text内容
Close #1 '关闭
End Function

'防止 记事本 乱码
Private Function ReadUTF8(Path As String)
Dim EncodingMode As String '编码模式
Dim arrBinary() As Byte
EncodingMode = “UTF-8”
Open Path For Binary As #1
ReDim arrBinary(LOF(1) - 1)
Get #1, , arrBinary()
Close #1
ReadUTF8 = BytesToBstr(arrBinary, EncodingMode)
End Function

Private Function ReadANSI(Path As String) '普通模式 GB2312
Dim EncodingMode As String '编码模式
Dim arrBinary() As Byte
EncodingMode = “GB2312”
Open Path For Binary As #1
ReDim arrBinary(LOF(1) - 1)
Get #1, , arrBinary()
Close #1
ReadANSI = BytesToBstr(arrBinary, EncodingMode)
End Function

Private Function BytesToBstr(Binary, Unicode)
Dim objstream As Object
Set objstream = CreateObject(“ADODB.Stream”)
objstream.Type = 1
objstream.Mode = 3
objstream.Open
objstream.Write Binary
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Unicode
BytesToBstr = objstream.ReadText
objstream.Close
End Function

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

键盘上的舞指

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值