0

我就是这样,当我启用检查按钮时,某行文本会发生变化。这是我到目前为止所拥有的:

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles    CheckBox1.CheckedChanged
    FileOpen(1, "C:\ServerMaker\Vanilla\server.properties", OpenMode.Output)
    If CheckBox1.Checked Then
        FileSystem.WriteLine("")
    End If
End Sub

我希望第 7 行更改为文本“allow-flight=true”,如果未选中,我希望它为“allow-flight=false”

4

1 回答 1

1

因为truefalse有不同的长度,你必须先阅读所有的行。然后就可以编写修改后的文件了:

Dim lines As New List(Of String)
'Read the current contents
Using file = System.IO.File.OpenText("server.properties")
    While Not file.EndOfStream
        lines.Add(file.ReadLine)
    End While
End Using
'Write the modified contents
Using file As New StreamWriter("server.properties")
    For i As Integer = 0 To lines.Count - 1
        If i = 6 Then
            file.WriteLine("allow-flight=" & IIf(CheckBox1.Checked, "true", "false"))
        Else
            file.WriteLine(lines(i))
        End If
    Next
End Using

检查If i = 6可能应该是If lines(i).StartsWith("allow-flight=")允许该行位于其他地方。

于 2013-09-28T16:38:50.460 回答