请教在VB.net中如何将数据写入txt文件、再从txt文件读出?软糖来告诉你吧 。
VB.net中读写文件主要使用System.IO命名空间 。
① 使用 File.ReadAllText 读取
Dim s As String = System.IO.File.ReadAllText("C:\a.txt")
② 使用 StreamReader 读取,注意编码格式和写入的编码保持一致 。
Dim sr As StreamReader = New StreamReader("C:\a.txt", System.Text.Encoding.UTF8)
Dim s As String = sr.ReadToEnd()
sr.Close()
③ 使用 File.WriteAllText 写入,会覆盖同名的文件 。
Dim 要写的内容 As String = ""
File.WriteAllText(文件路径, 要写的内容, System.Text.Encoding.UTF8)
④ 使用 StreamWriter 写入 。
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter("C:\a.txt", False,System.Text.Encoding.UTF8)
sw.WriteLine(TextTB.Text)
sw.Close()
⑤ 使用 StreamWriter 追加写入 。
将上面代码的第二个参数False改为True 。
◆ 满意请采纳,谢谢 ◆
如何用vb.net编写读取txt内容的代码?窗体上添加2个文本框,设置成多行,2个按钮,在文本框1里随便输入若干文字 , 可以多行,单击按钮1 , 保存到文件 。然后单击按钮2 , 把刚才写入的文件读到文本框2里 。
代码如下:
'写文本文件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'创建(写入)一个文本文件
Dim MyStream As New System.IO.FileStream(Application.StartupPath"\Ssk.txt", System.IO.FileMode.Create)
Dim MyWriter As New System.IO.StreamWriter(MyStream, System.Text.Encoding.Default)
MyWriter.WriteLine(TextBox1.Text)
MyWriter.Flush()
MyWriter.Close()
MyStream.Close()
End Sub
'读文本文件
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'读取一个文本文件
Dim MyReader As New System.IO.StreamReader(Application.StartupPath"\Ssk.txt", System.Text.Encoding.UTF8)
TextBox2.Text = MyReader.ReadToEnd()
MyReader.Close()
End Sub
气斜射入水或其他介质,折射光线与入射光线法线在
vb.net读写txt文件的问题这个用读写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)
推荐阅读
- jquery和html区别,jquery中html与val
- chatgpt俄乌战争谁会赢,chatGpT在俄乌冲突
- 竞速体育游戏,竞技体育游戏完整版
- pdf浏览记录如何查找,pdf浏览记录怎么删除
- 数据库怎么设计mysql 数据库怎么设计表结构
- sqlserver2014下载安装,sqlserver 下载
- ipad飞行游戏,ipad飞行游戏3d
- 斗鱼diss直播,斗鱼直播梗
- c语言带参数的宏和函数 c语言带参数的宏和函数怎么写