Finding and Deleting a Line.

Michaelk

Well-known member
Joined
Jun 3, 2004
Messages
58
Location
Australia
Programming Experience
3-5
Hi. I've got a txt file with a list of websites. A new site on each line. In my app i've got a textbox, and a button. Once the button is pressed i'd like the app to read through the lines of the txt file untill it finds an exact match of the textbox, to a line in the txt file. Then i'd like the line removed.
I've been trying to use the text.trim, text.remove etc commands, but these ask for an integer position in the txt file to start and stop from, and i've got no idea how to do this.
Does anyone know how i can achieve this?

Thanks in advance.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
If you don't need to visually display the text file, you could read it into a string and use string functions (like String.Replace) to manipulate the string, then rewrite the file.
As you've seen, dealing directly with the text file is more difficult.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
tonight i'll write up a quick example on how to stick it into a collection, or you could use paszt's string sugestion
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
VB.NET:
 Option Explicit On 
 Option Strict On
 
 Imports System.IO
 
 Private MyCol As New ArrayList 'module/form level variable
 
 Friend Sub ReadInfo()
 		Const EOF As Integer = -1
 		Dim srOpenInfo As New StreamReader("D:\text.txt")
 		Do While srOpenInfo.Peek <> EOF
 			MyCol.Add(srOpenInfo.ReadLine())
 		Loop
 		srOpenInfo.Close()
 	End Sub
 
 	Friend Sub WriteInfo()
 		Dim strWebSite As String
 		Dim swOpenInfo As New StreamWriter("D:\text.txt")
 		For Each strWebSite In MyCol
 			swOpenInfo.WriteLine(strWebSite)
 		Next
 		swOpenInfo.Close()
 	End Sub

what this does is read all the info from the file into the collection, is which you can add/remove items from it and then write it from the collection to the file again, pretty straight forward
 
Top Bottom