我需要在 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 文件中删除,而不会留下空白或摆脱空行的方法。
谢谢!