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

相关推荐
Metaphor6925 天前
Python 实现 Word/TXT 互转:附完整代码
python·word·格式转换·txt
专注VB编程开发20年6 个月前
VBA/VB6连接、读取Mdb access数据库最快的方法
前端·ui·ado·vb6
专注VB编程开发20年6 个月前
24位位图BMP内存像素顺序是bgr,转成32位处理更快
vb6·多媒体开发
专注VB编程开发20年6 个月前
C#WEBVBIEW2单点监听,多点分发;异步发起,信号唤醒
vb6·多媒体开发
luffy54598 个月前
txt文件所有数据在一列如何转多行多列
windows·excel·txt·一列转多行·一列
課代表10 个月前
bat 批处理从文本文件自动创建文件夹
自动化·脚本·bat·批处理·txt·文件编码·文件夹创建
专注VB编程开发20年10 个月前
VB编程语言最大源码项目VFB,photoDemon(类似PS的图像处理软件)
开源·vb6·photodemon
清水迎朝阳1 年前
有声小说书屋使用教程
注释·txt·有声小说·听书·有声小说书屋
专注VB编程开发20年1 年前
JSA变成类似vba环境给第三方软件集成IDE功能,脚本功能
ide·microsoft·node.js·vba·wps·vb6·jsa
专注VB编程开发20年1 年前
VB.NET2003和VB2008可以导入VB6项目
.net·vb.net·vb6·vb2008