十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
软糖来告诉你吧。
成都创新互联主营渑池网站建设的网络公司,主营网站建设方案,app软件开发,渑池h5成都微信小程序搭建,渑池网站营销推广欢迎渑池等地区企业咨询
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文本文档中的数据,并把数据保存为数组,代码如下:
'写配件文件
Private Sub saveIni(ByVal filePath As String, ByVal str As String)
Dim sw As StreamWriter = New StreamWriter(filePath, True) 'true是指以追加的方式打开指定文件
sw.WriteLine(str)
sw.Flush()
sw.Close()
sw = Nothing
End Sub
'读配件文件
Private Function readIni(ByVal filePath As String)
Dim iniDt As New DataTable
iniDt.Columns.Add("text")
iniDt.Columns.Add("value")
Try
Dim sr As StreamReader = New StreamReader(filePath, System.Text.Encoding.Default)
Dim line As String = ""
While Not sr.EndOfStream
Dim str = sr.ReadLine()'读取当前行
iniDt.Rows.Add(New String() {
str(0),
str(1)
})
End While
sr.Close()
sr = Nothing
Catch ex As Exception
End Try
Return iniDt
End Function
窗体上添加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
气斜射入水或其他介质,折射光线与入射光线法线在
这个用读写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
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.Filter = "所有Txt文件|*.txt|所有文件|*.*"
OpenFileDialog1.Title = "数据文件"
Dim filename As String
filename = ""
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
filename = OpenFileDialog1.FileName
End If
Dim StrInfo As String
Dim i As Integer
Dim ReadStream As New System.IO.StreamReader(filename, System.Text.Encoding.Default)
If ReadStream IsNot Nothing Then
Do Until ReadStream.EndOfStream
StrInfo = ReadStream.ReadLine()
Dim a() As String = StrInfo.Split("-")
ListView1.Items.Add(a(0))
ListView1.Items(i).SubItems.Add(a(1))
i = i + 1
Loop
ReadStream.Close()
End If
End Sub