我需要在vb中设置这段代码,以便用户可以从.txt文档中删除特定字符串。但是当他们这样做时,它会留下空洞。然后我得到了更多代码来阻止它,但随后它用空行删除了文件中的其他内容。
代码:
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
文字档案:
admin
guest
testing
你可以查看我的代码,或者给我一些代码,允许我从.txt文件中删除,输入到文本框中,而不留空或者去除空行。< / p>
谢谢!
答案 0 :(得分:1)
您可以将要写回的项目存储在另一个列表linesToKeep
中的文件中,如下所示:
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
更新:
以下是使用此解决方案应用于已发布代码的完整代码:
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