Question Editing text within a file

stezaman

New member
Joined
Apr 8, 2010
Messages
2
Programming Experience
1-3
I need a functionality to edit a text file. I like to edit a specific content within a file. As an example, a text file contains following text:

Text File said:
URL: http://www.gmail.com/
UserID: MyUserName
Password: MyPassWord

URL: http://www.mail.yahoo.com/
UserID: MyUserName
Password: MyPassWord

URL: Visual Basic .NET Forums - The Exclusive VB.NET Community
UserID: MyUserName
Password: MyPassWord
I like to edit UserID of http://www.mail.yahoo.com/ and after replacement file should be as follows:

Text File said:
URL: http://www.gmail.com/
UserID: MyUserName
Password: MyPassWord


URL: http://www.mail.yahoo.com/
UserID: MyChangeUserID
Password: MyPassWord

URL: Visual Basic .NET Forums - The Exclusive VB.NET Community
UserID: MyUserName
Password: MyPassWord

Please, give me some functionality how to solve this problem.

Kind regards,

stezaman
 
You can't really edit a text file literally. You need to read the data in, edit it, then write it all out again. You can call IO.File.ReadAllText to read the file into a single String, or call ReadAllLines to read the lines into a String array. You can then edit the String(s) as desired. Finally, you would call WriteAllText or WriteAllLines to save the new data back to the file.

That said, given the data you have, you might be better off to use an XML file. .NET has lots of built-in XML-handling functionality.
 
Back
Top