We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,577 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Writing in b/w text file

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

2
Contributors
2
Replies
1 Day
Discussion Span
2 Years Ago
Last Updated
3
Views
Question
Answered
battlex2010
Newbie Poster
15 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

codeorder
Postaholic
2,124 posts since Aug 2010
Reputation Points: 256
Solved Threads: 387
Skill Endorsements: 8

Thank you ver much!! :D

battlex2010
Newbie Poster
15 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 2 Years Ago by codeorder

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.2974 seconds using 2.64MB