C# File AppendAllLines() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

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.

Syntax

Pass the path and the lines to add.

C#
string path = Path.Combine(Path.GetTempPath(), "codetofun-appendalllines.txt");

File.AppendAllLines(path, new[] { "first", "second" });
File.AppendAllLines(path, new[] { "third" });

Console.WriteLine(File.ReadAllLines(path).Length);
CallWhat it does
File.AppendAllLines(path, lines)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

ParameterDescription
pathThe file to append to. Created if missing; its folder must exist.
contentsThe lines to add. It must not be null.
encodingOptional. Defaults to UTF-8 without BOM.

Return Value

Return ValueDescription
voidNothing 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);
    }
}

How It Works

  1. The first round creates the file. It did not exist, so AppendAllLines made it and wrote two lines.
  2. The second round adds. The old two lines stay, and two more come after them.
  3. 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);
    }
}

How It Works

  1. The old text is open-ended. WriteAllText saved a with no line break after it.
  2. Append starts right there. AppendAllLines writes b and then a line break, but it does not add a break before the new line.
  3. 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);
    }
}

How It Works

  1. A mixed list. Four events, and two of them start with ERR.
  2. A sequence is accepted. Where returns an IEnumerable<string>, which is what AppendAllLines wants. No array or list is built first.
  3. 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.

Next: AppendAllText()

Add a string to the end of a file, creating the file if it is missing.

AppendAllText() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful