How do you programatically open a file in c# to append?

Xarzu

Newcomer
Joined
Jan 9, 2008
Messages
2
How do you programatically open a file in c# to append?

I tried searching online for an example and so far I have not found anything.

There is System.IO.StreamWriter and there is System.IO.StreamReader but there is no StreamAppend. Is there some way to use StreamWriter without overwritting the content of the existing file? Is there some way to use System.IO.Stream with some sort of appending criteria?
 
One (or more) of the FileStream constructors accepts a FileMode argument, which allows you to specify FileMode.Append. Once you've created the FileStream, you can create a StreamWriter with the FileStream.

Depending on what you're doing, you might also be able to use one of the handy convenience methods of the System.IO.File class.
Code:
void AppendToLog(string text) {
    File.AppendAllText(Program.LogPath, text);
}
 
Back
Top