I am using streamreader to read and streamwriter to write,

i have put the lines into an array
[text file contents]

********** Remove [ ] *****************
blah
blah
blah
;this is blah
blah
blah
;this is another blah
[end of example file]
i want to remove the lines that start with "****" and keep deleting lines until i reach ";"
i thought this would be simple but im missing something..
and i dont want to put this array into a listbox it is being writen to a text file.

thank you

Member Avatar for Unhnd_Exception

See if this helps.

With the text file above this code changes it to:

;this is blah
blah
blah
;this is another blah

Dim fileName As String = "c:\blah.txt"

If Not My.Computer.FileSystem.FileExists(fileName) Then Exit Sub

Dim Lines As String = My.Computer.FileSystem.ReadAllText(fileName)

Dim startIndex As Integer = Lines.IndexOf("****")

If startIndex = -1 Then Exit Sub

Dim stopIndex As Integer = Lines.IndexOf(";", startIndex)

If stopIndex = -1 Then Exit Sub

Lines = Lines.Remove(startIndex, stopIndex - startIndex)

My.Computer.FileSystem.WriteAllText(fileName, Lines, False)

Also, see if this helps.

Public Class Form1
    Private myFile As String = "C:\test.txt" '// your File to Load/Edit if needed/Save.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If IO.File.Exists(myFile) Then '// check if File Exists.
            Dim arTemp() As String = IO.File.ReadAllLines(myFile) '// Load File Lines in a String Array.
            Dim sTemp As String = Nothing '// String used to keep only selected File lines.
            Dim bKeepFileLines As String = True '// determine if worth keeping the remaining File Lines or Not.
            For Each fileLine As String In arTemp '// Loop thru all File Lines in the String Array.
                If fileLine.StartsWith("****") Then bKeepFileLines = False '// check if line.StartsWith... and set to False NOT to keep the File Lines.
                If fileLine.StartsWith(";") Then bKeepFileLines = True '// once Locating the ";", it sets to Keep the remaining File Lines.
                If bKeepFileLines = True Then '// if set to True, keep the File Lines.
                    If Not sTemp = Nothing Then '// check if Not Empty, add a Line Break and the Line Content.
                        sTemp &= vbNewLine & fileLine
                    Else '// add only the Line Content.
                        sTemp = fileLine
                    End If
                End If
            Next
            IO.File.WriteAllText(myFile, sTemp) '// Save File back: IO.File.WriteAllText(File Name, File Content to Save)
            MsgBox("File Edited and Saved.", MsgBoxStyle.Information) '// Display confirmation that File has been modified and Saved.
        Else
            MsgBox("File Does Not Exist.", MsgBoxStyle.Critical) '// Display confirmation if File Does Not Exist.
        End If
    End Sub
End Class
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.