Finding and Deleting a Line.

Michaelk

Well-known member
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?

Staff member

Michaelk

Well-known member
Thanks for the reply. I had a look at both of the articles, but i've honestly got no idea where to start. I'm currently reading the txt file into a multiline textbox, is this the way to go? Or should i deal directly with the txt file?
Thanks.

Paszt

Staff member
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
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
VB.NET:
 Option Explicit On
Option Strict On

Imports System.IO

Private MyCol As New ArrayList 'module/form level variable

Const EOF As Integer = -1
Do While srOpenInfo.Peek <> EOF
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

Replies
5
Views
217
Replies
4
Views
455
Replies
0
Views
149
Replies
1
Views
34
Replies
4
Views
1K