Appending to a File

To add to the end of a file instead of replacing it, open a StreamWriter with append set to true, or use File.AppendAllText. Either creates the file if it isn't there yet.

Journeyman · C# · Updated

After this lesson you can

  • Add lines to the end of a file with new StreamWriter(path, append: true) or File.AppendAllText, keeping what's already there

Key takeaways

  • new StreamWriter(path, append: true) adds to the end of a file and keeps everything already there.
  • File.AppendAllText adds no line break of its own, so include Environment.NewLine; File.AppendAllLines puts each string on its own line.
  • Appending creates the file if it doesn't exist yet, so the very first append works too.
  • Replace the file to save the whole list again; append to add one new record or one more line to a log.

A new student joins the class. You don't want to rewrite the whole file to add one line; you want to append it, adding to the end and keeping everything already there.

append: true

StreamWriter has a second parameter, append. Pass true, and the writer starts at the end of the file instead of replacing it:

File.WriteAllLines("student_grades.csv", new[] { "firstname,lastname,gpa", "Emily,Wilson,3.91" });

using (StreamWriter writer = new StreamWriter("student_grades.csv", append: true))
{
    writer.WriteLine("Zoe,Whitehouse,3.95");
}

Console.WriteLine(File.ReadAllText("student_grades.csv"));
firstname,lastname,gpa
Emily,Wilson,3.91
Zoe,Whitehouse,3.95

Writing append: true with the parameter's name isn't required (new StreamWriter(path, true) does the same), but it tells every reader what that true means.

If the file doesn't exist yet, appending creates it, so the very first append works too.

The shortcuts

File has append versions of its one-line writers:

Method Adds to the end of the file
File.AppendAllText(path, text) One string, as it is: include Environment.NewLine or \n if you want a line break
File.AppendAllLines(path, lines) Each string in a list or array, each on its own line
File.WriteAllText("visits.txt", "");

File.AppendAllText("visits.txt", "Monday: 40 visitors" + Environment.NewLine);
File.AppendAllText("visits.txt", "Tuesday: 55 visitors" + Environment.NewLine);
File.AppendAllLines("visits.txt", new[] { "Wednesday: 38 visitors", "Thursday: 61 visitors" });

Console.WriteLine(File.ReadAllText("visits.txt"));
Monday: 40 visitors
Tuesday: 55 visitors
Wednesday: 38 visitors
Thursday: 61 visitors

Replace or append?

You want to Use
Save the whole list again, such as after editing a student new StreamWriter(path) or File.WriteAllLines: replace
Add one new record, or one more line to a log new StreamWriter(path, append: true) or File.AppendAllText: append

Appending is exactly what a log file needs: every event is one more line at the end, and nothing written before is ever lost. That's the next topic.

Check yourself

1 question to try on the lesson page.

So what

append: true turns "replace the file" into "add to the end". It's one small parameter, and choosing the wrong one either loses data or duplicates it, so choose on purpose.

Key terms

Cheat sheets