Reading from a text file

teamdad

Member
Joined
Dec 31, 2003
Messages
12
I use the following code to write individual lines into a text file:

Dim m_strTestFile As String = "c:\test.txt"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim writer As New IO.StreamWriter(m_strTestFile)

writer.Write(TextBox1.Text & vbCrLf)
writer.Write(TextBox2.Text & vbCrLf)
writer.Write(TextBox3.Text & vbCrLf)
writer.Write(TextBox4.Text & vbCrLf)
writer.Flush()
writer.Close()

End Sub

The test.text file looks like this with the text that has been typed in the boxes.

Text Box Line 1
Text Box Line 2
Text Box Line 3
Text Box Line 4

If say the boxes were reset and did not have text in them any longer, how can I use the IO.StreamReader to recall the individual lines back into the text boxes they were written in originally with another button click? Would it be easier to use an xml format and if so could someone give an example of both read and write to get me started in that direction?

Thanks in advance
 
also you could have used writer.WriteLine(.....) instead of appending the linefeed yourself - this would aid portability to other .Net languages which may not support the VB constants.
 
Back
Top