在VB 2017中替换文本文件中的多行

时间:2018-10-12 01:12:40

标签: vb.net

我在这里有些卡住,我想做的是阅读一个文本文件,找到包含X =和Y =的行,然后将X =更改为X = 1,将Y =更改为Y = 1

到目前为止,我已经获得了这段代码

    Dim MyPath As String = "C:\Users\kiko4\Desktop\text.txt"
    Dim Rdr As New StreamReader(MyPath)
    Dim ln As String
    Dim NewFile As New StringBuilder

    ln = Rdr.ReadLine

    Do Until ln Is Nothing

        If ln.StartsWith("X=", "Y=") And ComboBox1.Text = "1" Then ln = "X=1" & "Y=1"
        NewFile.AppendLine(ln)
        ln = Rdr.ReadLine
    Loop

    Rdr.Close()

    File.WriteAllText(MyPath, NewFile.ToString)
    MsgBox("Successfully changed the resolution.", MsgBoxStyle.Information)

End Sub

因此这导致仅更改X并保持原样保留Y,是否可以同时更改两行?

3 个答案:

答案 0 :(得分:0)

这就是我固定的方式,可以按预期工作,同时更改了两个值,感谢大家的帮助:)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim MyPath As String "C:\Users\kiko4\AppData\Local\FortniteGame\Saved\Config\WindowsClient\text.txt")
        Dim Rdr As New StreamReader(MyPath)
        Dim ln As String
        Dim ac As String
        Dim NewFile As New StringBuilder

        ln = Rdr.ReadLine
        ac = Rdr.ReadLine
        Do Until ln Is Nothing
            If ln.StartsWith("ResolutionSizeX=") Then ln = "ResolutionSizeX=" + TextBox1.Text
            NewFile.AppendLine(ln)
            ln = Rdr.ReadLine
            If ac.StartsWith("ResolutionSizeY=") Then ac = "ResolutionSizeY=" + TextBox2.Text
            NewFile.AppendLine(ac)
            ac = Rdr.ReadLine
        Loop
        Rdr.Close()
        File.WriteAllText(MyPath, NewFile.ToString)
        MsgBox("Successfully changed the resolution.",  MsgBoxStyle.Information)

    End Sub
End Class

答案 1 :(得分:0)

在遇到困难时,尝试采用其他方法...试试:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim SR As New StreamReader("C:\Users\kiko4\Desktop\text.txt")
    Dim Result As String = SR.ReadToEnd()
    SR.Close()

    Dim Lines As String() = Result.Split(Environment.NewLine)

    Dim TestLine As String
    Dim ReplacedLine As String

    For Each line As String In Lines
        TestLine = line
        If TestLine.Contains("X=") Then
            ReplacedLine = TestLine.Replace("X=", "X=" & ComboBox1.Text)
            Result = Result.Replace(TestLine, ReplacedLine)
        ElseIf TestLine.Contains("Y=") Then
            ReplacedLine = TestLine.Replace("Y=", "Y=" & ComboBox1.Text)
            Result = Result.Replace(TestLine, ReplacedLine)
        End If
    Next

    Dim SW As New StreamWriter("C:\Users\kiko4\Desktop\text.txt")
    SW.Write(Result)
    SW.Close()
End Sub

请随时询问您不了解的任何事情。

有用的希望

答案 2 :(得分:0)

使用StreamReaders / Writers很不错,而且肯定是最好的方法 当您尝试一次遍历多行时。 但是,在这种情况下,这实际上不是必需的,因为OP只是尝试替换文件中的所有事件,所以行根本没有关系。

从可读性/长度的角度来看,最好是这样:

    Dim inPath As String = "C:\Users\kiko4\Desktop\text.txt"
    Dim trText As String = File.ReadAllText(inPath)
    If trText.Contains("X=") AndAlso trText.Contains("Y=") Then File.WriteAllText(inPath, trText.Replace("X=", "X=1").Replace("Y=", "Y=1"))

(读取文件中的所有文本并将其作为单个字符串保存到trText,如果trText包含要替换的字符串,则将具有替换值的trText的内容写入文件)

或者,如果您100%确保所传递的每个文件都包含搜索值,那么您甚至可以在一行中完成该操作:

File.WriteAllText("C:\Users\kiko4\Desktop\text.txt", File.ReadAllText("C:\Users\kiko4\Desktop\text.txt").Replace("X=", "X=1").Replace("Y=", "Y=1"))

如果文件中不包含“ X =”和“ Y =”,则该爆炸。

相关问题