You'll need to use the FileStream
class to open the file, the StreamWriter
class to read from the file, and the StreamWriter
class to write back to the file.
You can create a FileStream like this:
FileStream file = new FileStream("FileName", FileMode.Open, FileAccess.ReadWrite);
Then wrap the FileStream
in a StreamReader
:
StreamReader reader = new StreamReader(file);
Then, read in each line and do your string processing (adding commas and brackets):
while(reader.EndOfFile)
{
string currentLine = reader.ReadLine();
// do your string processing here and save the result somewhere
}
Lastly, wrap the FileStream
in a StreamWriter
and write your modified strings back to the file:
StreamWriter writer = new StreamWriter(file);
// Write your content here
writer.Write("my content");
Don't forget to close your streams after working with them.
reader.Close();
writer.Close();
file.Close();