0

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

谢谢!

4

1 回答 1

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
于 2013-08-31T03:03:07.420 回答