Read, and write to a text file

decrypt

Well-known member
Joined
Oct 6, 2003
Messages
216
How could i read and write to a text file? ie. read it, edit the information manually, save it...
 
How would i get this:
Code:
	Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

		Dim line As String = ""

		Dim SR As New IO.StreamReader(TextBox2.Text)

		Dim SB As System.Text.StringBuilder = New System.Text.StringBuilder
		Do
			line = SR.ReadLine
			If line <> TextBox4.Text Then
				SB.Append(line)
				SB.Append(System.Environment.NewLine)
			End If
		Loop Until line = ""
		SR.Close()

		Dim SW As New IO.StreamWriter(TextBox2.Text, False)

		SW.Write(SB.ToString())
		SW.Close()

	End Sub

To read every line?

Also how would i read this from a file? (.txt file)

I want it to read the whole file and display it in a textbox... (like what notepad does)
 
this doesnt seem to be working:
Code:
        Dim oFile As System.IO.File
        Dim oRead As System.IO.StreamReader
        oRead = oFile.OpenText("D:\Image.txt")
        Dim EntireFile As String
        EntireFile = oRead.ReadToEnd()
        TextBox2.Text = EntireFile
 
Any hints as to what you mean by doesnt seem to be working? Does it give any errors? Does the file exist? Is anything else using the file?
 
dim sr as streamreader = new streamreader("c:\test.txt")
do until sr.peek =< 0
debug.writeline sr.readline
loop

if you want to whole file at once you could try binary mode
 
ive been wondering this for a while... what does debug.writeline do?

Is there anyway to change the debug.writeline to something that with textbox1.text...
 
Yay, i got it working, i just mixed the two together:

Code:
        Dim line As String = ""
        Dim sr As System.IO.StreamReader = New System.IO.StreamReader("C:/text.txt")
        Do             
            line = sr.ReadLine
            If line <> TextBox2.Text Then
                TextBox2.AppendText(line)
                TextBox2.AppendText(System.Environment.NewLine)
            End If
        Loop Until line = ""

Do you know how i could do this now by writing to a file?
 
Last edited by a moderator:
debug.writeline() writes a string to the Output Window followed with a carriage return/line feed

its used for debugging your app during runtime.
 
Iceplug said:
How could you do what by writing to a file?
To write to a file, use StreamWriter instead of StreamReader. :)

i just want to know how you can write text to a text file, simply...
 
decrypt said:
i just want to know how you can write text to a text file, simply...

Code:
Dim SW As New IO.StreamWriter("where to write") 
        SW.WriteLine("what to write")  example: TextBox1.Text
        SW.Close()
 
Back
Top