0

ユーザーが .txt ドキュメントから特定の文字列を削除できるように、このコードを vb に設定する必要があります。しかし、そうすると、空の行が残ります。次に、これを停止するためのコードをさらに取得しましたが、ファイル内の空の行で何か他のものを削除します。

コード:

        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 に答える