vb.net读写txt文件的问题
txt文件内容为:v1=133233v2=132387v3=322333我想程序点button1时就读取txt文件,textbox1.text=v1的值(即133233)...
txt文件内容为:
v1=133233
v2=132387
v3=322333
我想程序点button1时就读取txt文件 ,textbox1.text=v1的值(即133233)
代码要怎么写?谢 展开
v1=133233
v2=132387
v3=322333
我想程序点button1时就读取txt文件 ,textbox1.text=v1的值(即133233)
代码要怎么写?谢 展开
2个回答
展开全部
这个用读写TXT的话比较繁琐,建议题主使用读写INI的方式,参考:
'声明INI配置文件读写API函数
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32
'定义读取配置文件函数
Public Function GetINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As String
Dim Str As String = LSet(Str, 256)
GetPrivateProfileString(Section, AppName, lpDefault, Str, Len(Str), FileName)
Return Microsoft.VisualBasic.Left(Str, InStr(Str, Chr(0)) - 1)
End Function
'定义写入配置文件函数
Public Function WriteINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As Long
WriteINI = WritePrivateProfileString(Section, AppName, lpDefault, FileName)
End Function
'定义INI文件位置
Dim path As String
path = "C:\t861239263.ini" '假设是这个文件
那么读取的时候:
TextBox1.Text = GetINI("main","v1","", path)
TextBox2.Text = GetINI("main","v2","", path)
TextBox3.Text = GetINI("main","v3","", path)
写入的时候:
WriteINI("main","v1", TextBox1.Text, path)
WriteINI("main","v2", TextBox2.Text, path)
WriteINI("main","v3", TextBox3.Text, path)
【附】INI文件的格式,范例:
[main]
v1=133233
v2=132387
v3=322333
[aaa]
abc=1
bcd=2
[bbb]
ni=wo
wo=ta
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询