In C#, File.AppendAllLines() adds a collection of strings to the end of a file, with a line break after each string. The file is created if it does not exist, and old content is kept.
The collection can be a string[] or any IEnumerable<string>. It saves you from adding Environment.NewLine to each entry yourself, as AppendAllText would need.
This is a static method of System.IO.File. Call it as File.AppendAllLines(...); there is no new File(). For an object that remembers one file, see FileInfo.
Adds each string as a line at the end of the file.
File.AppendAllLines(path, lines, encoding)
Same, with the encoding you choose.
File.AppendAllLines(path, list.Where(...))
Adds only the items that pass a LINQ filter.
AppendAllLines opens the file once for the whole collection. For text without line breaks use AppendAllText, and for step-by-step writing use AppendText.
Library declarations
C#
public static void AppendAllLines(string path, IEnumerable<string> contents);
public static void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding);
Parameters
Parameter
Description
path
The file to append to. Created if missing; its folder must exist.
contents
The lines to add. It must not be null.
encoding
Optional. Defaults to UTF-8 without BOM.
Return Value
Return Value
Description
void
Nothing is returned. The file ends with the new lines.
C# Examples
Example 1 — Append Lines in Two Rounds
Add two lines, then two more, and read the whole file.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-appendalllines");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "names.txt");
File.AppendAllLines(path, new[] { "Ann", "Bob" });
File.AppendAllLines(path, new[] { "Cy", "Di" });
string[] lines = File.ReadAllLines(path);
Console.WriteLine(lines.Length);
Console.WriteLine(string.Join(" ", lines));
Directory.Delete(root, true);
}
}
Output
4
Ann Bob Cy Di
How It Works
The first round creates the file. It did not exist, so AppendAllLines made it and wrote two lines.
The second round adds. The old two lines stay, and two more come after them.
Four lines in order.ReadAllLines returns four items, and string.Join(" ", lines) shows them on one row.
Example 2 — When the Old File Has No Final Newline
Append a line to a file that was saved without a trailing line break.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-appendalllines-glue");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "tail.txt");
File.WriteAllText(path, "a");
File.AppendAllLines(path, new[] { "b" });
string[] lines = File.ReadAllLines(path);
Console.WriteLine(lines.Length);
Console.WriteLine(lines[0]);
Directory.Delete(root, true);
}
}
Output
1
ab
How It Works
The old text is open-ended.WriteAllText saved a with no line break after it.
Append starts right there.AppendAllLines writes b and then a line break, but it does not add a break before the new line.
One glued line. The file now has a single line, ab. The fix is to save the first part with WriteAllLines, or to start your own text with a break.
Example 3 — Append Only the Lines You Want
Filter a list with LINQ and append the matches to a report file.
C#
using System;
using System.IO;
using System.Linq;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-appendalllines-filter");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "errors.txt");
string[] events = { "OK start", "ERR disk", "OK save", "ERR net" };
File.AppendAllLines(path, events.Where(e => e.StartsWith("ERR")));
string[] saved = File.ReadAllLines(path);
Console.WriteLine(saved.Length);
Console.WriteLine(saved[0]);
Directory.Delete(root, true);
}
}
Output
2
ERR disk
How It Works
A mixed list. Four events, and two of them start with ERR.
A sequence is accepted.Where returns an IEnumerable<string>, which is what AppendAllLines wants. No array or list is built first.
Only matches are saved. The file has 2 lines, and the first one is ERR disk.
Frequently Asked Questions
It adds a collection of strings to the end of a file, one line per string, and creates the file if it does not exist.
Yes. Every appended line ends with a line break, so the next append starts on a new line.
The old file did not end with a line break, so the new text continued that line. Write a break first, or save the old content with WriteAllLines.
Yes. Any IEnumerable<string> works: arrays, lists, HashSet<string>, or the result of a LINQ query.
WriteAllLines replaces the whole file. AppendAllLines keeps the old content and adds the new lines at the end.