Reputation: 650
I need to set this piece of code in vb so that a user can delete a specific string from a .txt document. But when they do, it leaves an empty line. I then got more code to stop this but then it deletes something else within the file with the empty line.
Code:
If (txtDelUser.Text = "admin" Or txtDelUser.Text = "guest") Then
rtbOutput2.Text = "You tried to delete: " + txtDelUser.Text + ". But the user is protected by default."
Else
Dim ln As Integer = 1
rtbOutput.Text.First(Of Text = Text.CopyTo.ClipBoard
'Removes the selected username from list of usernames
Dim lines As New List(Of String)
Using sr As New StreamReader("C:\ProgramData\Hax Client\User Data\All Usernames.txt")
While Not sr.EndOfStream
lines.Add(sr.ReadLine)
End While
End Using
For Each line As String In lines
If line.Contains(txtDelUser.Text) Then
lines.Remove(line)
Exit For 'must exit as we changed the iteration
End If
Next
'End of removing the line
Dim fs As New FileStream("C:\ProgramData\Hax Client\User Data\All Usernames.txt", FileMode.Append)
Dim sw As New StreamWriter(fs)
Dim foundBlank As Boolean
For Each line As String In lines
If line.Length > 0 Then
sw.WriteLine(line)
' Reset blank line flag
foundBlank = False
Else
If Not foundBlank Then
' Blank line: write first one
sw.WriteLine(line)
' Set flag to indicate that blank line was found
foundBlank = True
End If
End If
Next
sw.Close()
fs.Close()
My.Computer.FileSystem.FileExists("C:\ProgramData\Hax Client\User Data\" + txtDelUser.Text)
My.Computer.FileSystem.DeleteFile("C:\ProgramData\Hax Client\User Data\" + txtDelUser.Text + ".txt")
rtbOutput2.Text = "Deleted user: " + txtDelUser.Text
End If
Text file:
admin
guest
testing
Can you either look over my code or give me some code that will allow me to delete from the .txt file, by whats entered into a textbox, without leaving an empty or a way to get rid of the empty line.
Thanks!
Upvotes: 0
Views: 934
Reputation: 34846
You can store the items that you want to write back to file in another list, linesToKeep
, like this:
Dim linesToKeep As New List(Of String)
For Each line As String In lines
If Not line.Contains(txtDelUser.Text) Then
linesToKeep.Add(line)
End If
Next
' Write all lines in linesToKeep back to file
For Each line As String In lines
sw.WriteLine(line)
Next
UPDATE:
Here is what the full code should look like using this solution applied to the code posted:
If (txtDelUser.Text = "admin" Or txtDelUser.Text = "guest") Then
rtbOutput2.Text = "You tried to delete: " + txtDelUser.Text + ". But the user is protected by default."
Else
Dim ln As Integer = 1
rtbOutput.Text.First(Of Text = Text.CopyTo.ClipBoard
'Removes the selected username from list of usernames
Dim lines As New List(Of String)
Using sr As New StreamReader("C:\ProgramData\Hax Client\User Data\All Usernames.txt")
While Not sr.EndOfStream
lines.Add(sr.ReadLine)
End While
End Using
' Instead of removing items from the list we read from file
' we are going to keep track of what we want to keep and write back to file later
Dim linesToKeep As New List(Of String)
For Each line As String In lines
If Not line.Contains(txtDelUser.Text) Then
linesToKeep.Add(line)
End If
Next
Dim fs As New FileStream("C:\ProgramData\Hax Client\User Data\All Usernames.txt", FileMode.Append)
Dim sw As New StreamWriter(fs)
' Write all lines in linesToKeep back to file
For Each line As String In lines
sw.WriteLine(line)
Next
sw.Close()
fs.Close()
My.Computer.FileSystem.FileExists("C:\ProgramData\Hax Client\User Data\" + txtDelUser.Text)
My.Computer.FileSystem.DeleteFile("C:\ProgramData\Hax Client\User Data\" + txtDelUser.Text + ".txt")
rtbOutput2.Text = "Deleted user: " + txtDelUser.Text
End If
Upvotes: 1