How to read and write text files by VB.NET
This article mainly introduces VB.NET how to read and write text files, the article is very detailed, has a certain reference value, interested friends must read it!
To save the text to a file, create a FileStream-based StreamReader object, and then call the Write method to write the text you want to save to the file.
The following VB.NET read and write text file code prompts the user with SaveFileDialog to specify a file to save the contents of the TextBox1.
SaveFileDialog1.Filter = _
"Text Files | * .txt | All Files | *. *"
SaveFileDialog1.FilterIndex = 0
If SaveFileDialog1.ShowDialog =
DialogResult.OK Then
Dim FS As FileStream =
SaveFileDialog1.OpenFile
Dim SW As New StreamWriter (FS)
SW.Write (TextBox1.Text)
SW.Close ()
FS.Close ()
End If
VB.NET can also read and write a text file, using a similar statement, we read a text file and display the contents in the TextBox control. The ReadToEnd method of StreamReader returns the entire contents of the file.
OpenFileDialog1.Filter = _
"Text Files | * .txt | All Files | *. *"
OpenFileDialog1.FilterIndex = 0
If OpenFileDialog1.ShowDialog
= DialogResult.OK Then
Dim FS As FileStream
FS = OpenFileDialog1.OpenFile
Dim SR As New StreamReader (FS)
TextBox1.Text = SR.ReadToEnd
SR.Close ()
FS.Close ()
End If
The above is all the contents of the article "how to read and write text files in VB.NET". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!