Editing a value inside a text document

Naldo

Newcomer
Joined
Aug 7, 2009
Messages
1
Hi new to the forum and programming with vb2008, hope u can help. :D

I have a text document that stores around 200 commands ,each on its own line with a value of either 0 or 1 after them. This determins if that command is ON or OFF.

I need to be able to manipulate the on/off values for 5 specific commands using a check box to say "turn this one on" if box is marked. or vice-versa.

ie in the text file

test_example1 0
test_example4 1
test_example3 1
test_example2 0
test_example9 0


The 5 commands are not always on the same line in the text file, and this is the only way I could find to make this work. So is there a way i can get this to look for the text inside the file and change its state with a check box?

Thanks in advance if anyone can help.
 
I haven't tested this but its the jist of what you need to do: load the file, find what you need to change, change it, and save the file.
Visual Basic:
Dim lines As String() = System.IO.File.ReadAllLines(filename)

' Loop through lines
For I As Integer = 0 To Lines.Length - 1
    Dim line As String = lines(i)
   
    'Sparate command and value
    Dim parts As String() = line.Trim().Split(new Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)

    'Somewhat validate data
    If parts.Length = 2 Then
        'Check if its what were looking for
        If parts(0) = nameImLookingfor Then
            'Update value
            lines(i) = nameImLookingFor & " " & newValue.ToString()
            Return ' Stop looking
        End If
    End If
Next I

'When done, use File.WriteAllLines to update file.
 
Back
Top