Add/Remove to/from File

PureSc0pe

Well-known member
Joined
Mar 17, 2004
Messages
161
Location
Arizona
The first code writes the text to the end line, but if used more than once, it puts all of the added text on the same line. How do I make it write to its own line every time?

Also, how can the second code be used to delete a line of text. I want to have to type the exact line that you want deleted for it to work.

VB Code: Add to File

Dim file As String
Dim StreamWriter As New IO.StreamWriter(TextBox2.Text, True)
StreamWriter.Write(TextBox3.Text)
StreamWriter.Close()


VB Code: Delete from File

Dim file As String
Dim StreamWriter As New IO.StreamWriter(TextBox2.Text)
StreamWriter.Write(TextBox4.Text)
StreamWriter.Close()
 
With Sequential File Access streams, you dont actually "delete" a line from a file.
You just "conveniently forget to put it back in the file".

Youd want to have a Stream for Reading and then one for Writing.
You can use a StringBuilder also. (something like this)
Code:
Caption = SR.Readline()
[COLOR=Teal]Read a line from file.[/COLOR]
[COLOR=Blue]If[/COLOR] Caption <> "asdfghjkl" [COLOR=Blue]Then[/COLOR]
  SB.Append(Caption)
  SB.Append(System.Environment.NewLine)
[COLOR=Teal]Conveniently forget to write the line "asdfghjkl" back to file.[/COLOR]
[COLOR=Blue]End If
Loop[/COLOR]
SW.Write(SB.ToString())
[COLOR=Teal]Write the built string to file.[/COLOR]
 
Ive got most of what youve said here, but SB? I assume that is Stringbuilder as you mentioned earlier. However, StringBuilder is not showing up as a Variable or whatever. Anything in Bold has an error.

VB Code:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim file As String
Dim SR As New IO.StreamReader(TextBox2.Text)
Dim SW As New IO.StreamWriter(TextBox2.Text)
Dim SB As StringBuilder

file = (TextBox2.Text)
file = SR.ReadLine()

If file <> (TextBox4.Text) Then
SB.Append(file)
SB.Append(System.Environment.NewLine)

End If
Loop
SW.Write(SB.ToString())

End Sub
 
Last edited by a moderator:
The Code shows no errors, but when I run the application it says:
An unhandled exception of type System.IO.IOException occurred in mscorlib.dll. Additional information: The process cannot access the file "C:\list.txt" because it is being used by another process.
It highlights Dim SW As New IO.StreamWriter(TextBox2.text) as the problem. Is it saying that because its being used by StreamReader? If so, how would I fix that?


VB Code:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim file As String
Dim SR As New IO.StreamReader(TextBox2.Text)
Dim SW As New IO.StreamWriter(TextBox2.Text)
Dim SB As System.Text.StringBuilder = New System.Text.StringBuilder

file = (TextBox2.Text)
file = SR.ReadLine()

If file <> (TextBox4.Text) Then
SB.Append(file)
SB.Append(System.Environment.NewLine)

End If
SW.Write(SB.ToString())

End Sub
 
Oh, yeah, you need to close the streamreader before you can open the streamwriter.
So, your code will look something like this.
Dim SR As IO.StreamReader = New StreamReader(...)
Dim SW As IO.StreamWriter

SR.Close()
SW = New StreamWriter(...)
SW.Write(...)
SW.Close()
 
Ok, when I run it, it deletes everything from the file except the first line. I need it to keep everything except what is typed into the texbox.

VB Code:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim file As String
Dim SR As New IO.StreamReader(TextBox2.Text)

file = (TextBox2.Text)
file = SR.ReadLine()

SR.Close()

Dim SW As New IO.StreamWriter(TextBox2.Text)
Dim SB As System.Text.StringBuilder = New System.Text.StringBuilder

If file <> (TextBox4.Text) Then
SB.Append(file)
SB.Append(System.Environment.NewLine)
End If

SW.Write(SB.ToString())

SW.Close()
End Sub
 
You are opening your streamreader (SR) but then closing it after only 1 Readline. you would really need to do this in a loop. Try something like the following (not tested though so no promises)
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
 
Back
Top