包含vb.net写txt的词条

如何用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里面只有一行!Public Class Form1
Private Sub 写入文件_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 写入文件.Click
FileOpen(1, "D:\一行文件.txt", OpenMode.Output) ' Open file for output.
WriteLine(1, "想只保留文件为一行那你就写一行好了")' Print blank line to file.
FileClose(1)' Close file.
End Sub
Private Sub 读取文件_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 读取文件.Click
Dim TextLine As String
FileOpen(1, "D:\一行文件.txt", OpenMode.Input) ' Open file for output.
TextLine = LineInput(1)'读取一行就了
MsgBox(TextLine)
FileClose(1)
End Sub
End Class
最简单的方法 还有很多方法 自己研究吧亲希望能帮到你
请教在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的词条】关于vb.net写txt和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读