所以我有一个正在使用的程序,我需要读取.ini文件的行,然后将用户定义的文本写入该文件中的特定行。到目前为止,我的一些代码是:
Dim File As String = ".\TestFile.ini"
Dim FileText As String
Dim NewText As String = TextBox2.Text
Dim lines() As String
Dim separator() As String = {Environment.NewLine}
Dim x As Integer = 0
If My.Computer.FileSystem.FileExists(File) Then
FileText = My.Computer.FileSystem.ReadAllText(File)
lines = FileText.Split(separator, StringSplitOptions.RemoveEmptyEntries)
While x < lines.Length()
If lines(x).Contains("Nickname") Then
lines(x) = "Nickname" & "=" & TextBox2.Text
End If
NewText = NewText & lines(x) & Environment.NewLine
x = x + 1
End While
My.Computer.FileSystem.WriteAllText(File, NewText, False)
Else
MsgBox("File does not exist!")
End If
我要编辑的文件如下所示:
Don't edit this line
Don't edit this line
Don't edit this line
Don't edit this line
Nickname=test
Don't edit this line
Don't edit this line
Don't edit this line
我只想在第4行上编辑此单词“ test”,但是我执行的代码会执行此操作,但还会在文件开头添加我在TextBox2中输入的内容。像这样(假设我将HelloWorld放在TextBox2中):
HelloWorldDon't edit this line
Don't edit this line
Don't edit this line
Don't edit this line
Nickname=HelloWorld
Don't edit this line
Don't edit this line
Don't edit this line
有人知道我的代码有什么问题吗?抱歉,我对此很陌生。
Line 3: Dim NewText As String = TextBox2.Text
初始化NewText =“ HelloWorld”
然后,您添加了第一行:
Line 14: NewText = NewText & lines(0) & Environment.NewLine
NewText现在等于“ NewWorld”&lines(0)&NewLine
尝试Dim NewText = String.Empty
第3行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句