I know how to search a text file for a specific line, but what I don't know is then how to delete that line? Can anyone help?

My text file is set up as such:

021,Donovan,56 Eynesford Crescent,Bexley,SE5 1TR,09/08/1967,13 March 2012,Bronze
062,Fredrikson,6 Freil Road,Gravesend,GR9 TRB,12/06/1995,13 March 2012,Silver

So I know how to search for the 3 character integer at the beginning of each line, but how do I then delete this line?

I'm currently using this code

Imports System.IO
Public Class Form6

    Private Sub SearchBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchBtn.Click

        Dim WholeFile() As String = System.IO.File.ReadAllLines("C:\Documents and Settings\User\My Documents\Visual Studio 2008\Projects\Members.txt")
        Dim Search As String = IDFilter.Text & ","

        For Each line As String In Filter(WholeFile, Search)
            If line.StartsWith(IDFilter.Text) Then
                MsgBox("found " & line)
            End If

    End Sub
End Class

Recommended Answers

All 3 Replies

Please see this thread that you were posting in. The dictionary also supports a Remove method as in


You could then (using the example in that other thread)

System.IO.File.WriteAllLines(outputfilename, records.Values.ToArray)

which creates an array of strings from the remaining values in the dictionary and writes them to a file.

you can use replace option just replace the whole line with white space.


That approach will work as long as you

1) use ReadAllText instead of ReadAllLines
2) include the trailing vbCrLf and replace the line with ""

Otherwise you will be left with a blank line. And to locate the record to delete you'd have to search for

vbCrLf & ID & ","

and do a check for the special case where the record to delete is the first one (with no preceding vbCrLf). All in all, a less robust and less clear approach.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.