Hello every one
i have a text file containing following data(it is a config file for another program):
[pos]
100
[lastsave]
10
How do i replace the value of [pos] i.e 100 with 200??
Thanks
BattleX
Hello every one
i have a text file containing following data(it is a config file for another program):
[pos]
100
[lastsave]
10
How do i replace the value of [pos] i.e 100 with 200??
Thanks
BattleX
See if this helps.
Public Class Form1
Private myCoolConfigFile As String = "C:\test.txt" '// your File to edit.
Private arTemp() As String = Nothing '// String Array to load/modify/save File lines.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If IO.File.Exists(myCoolConfigFile) Then
arTemp = IO.File.ReadAllLines(myCoolConfigFile) '// Load each File line as a array.
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'// editFileContent(line you want to locate, content used to modify the next line)
editFileContent("[pos]", TextBox1.Text)
End Sub
Private Sub editFileContent(ByVal lineContentToLocate As String, ByVal coolContentUsedToModifyNextLine As String)
If Not arTemp.Length = 0 Then '// check if File lines are loaded.
For i As Integer = 0 To arTemp.Length - 1 '// loop thru all lines.
If arTemp(i) = lineContentToLocate Then '// check if line equals the line you need to find.
arTemp(i + 1) = coolContentUsedToModifyNextLine '// change the next line in your array.
Exit For '// exit the loop since done editing.
End If
Next
IO.File.WriteAllLines(myCoolConfigFile, arTemp) '// Save modified File.
MsgBox("File edited and saved.")
End If
End Sub
End Class
If you need to edit another line, use: editFileContent("[lastsave]", TextBox1.Text)
. TextBox1.Text is the content to write to the file.
Thank you ver much!! :D
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.