C# File AppendAllLinesAsync() Method

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

Definition

In C#, File.AppendAllLinesAsync() adds each string of a sequence to the end of a file, one per line, then closes the file. If the file does not exist, it is created. It is the async version of File.AppendAllLines().

The lines come from an IEnumerable<string>, such as a string[] or a List<string>. Each line is followed by a line break, so you do not add one yourself.

It returns a plain Task. Use await so the next step runs after the lines have been added.

Syntax

Pass the path and the lines to add. An Encoding is optional.

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

await File.AppendAllLinesAsync(path, new[] { "one", "two" });
await File.AppendAllLinesAsync(path, new[] { "three" });

Console.WriteLine((await File.ReadAllLinesAsync(path)).Length);
CallWhat it does
await File.AppendAllLinesAsync(path, lines)Adds each string as a line at the end of the file, creating it if needed.
await File.AppendAllLinesAsync(path, lines, Encoding.Unicode)Adds the lines using the encoding you name.
await File.AppendAllLinesAsync(path, lines, token)Stops with an OperationCanceledException if the token is cancelled.

Use File.WriteAllLinesAsync when you want to replace the file instead of adding to it.

Library declarations
C#
public static Task AppendAllLinesAsync(string path, IEnumerable<string> contents, CancellationToken cancellationToken = default);
public static Task AppendAllLinesAsync(string path, IEnumerable<string> contents, Encoding encoding, CancellationToken cancellationToken = default);

Parameters

ParameterDescription
pathThe file to add to. It is created if it does not exist.
contentsThe strings to add, one per line. An array, a list, or any IEnumerable<string> works.
encodingOptional. The Encoding used to turn the text into bytes. Defaults to UTF-8 without a BOM.
cancellationTokenOptional. A token that lets you cancel the operation before it finishes.

Return Value

Return ValueDescription
TaskA task that completes when the lines have been added and the file is closed. There is no result value.

C# Examples

Example 1 — Append Lines to a New File

The file does not exist yet. Appending creates it and writes two lines.

C#
using System;
using System.IO;
using System.Threading.Tasks;

class Program {
    static async Task Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-appendalllinesasync");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string path = Path.Combine(root, "names.txt");

        await File.AppendAllLinesAsync(path, new[] { "one", "two" });

        string[] lines = await File.ReadAllLinesAsync(path);
        Console.WriteLine(lines.Length);
        Console.WriteLine(lines[1]);

        Directory.Delete(root, true);
    }
}

How It Works

  1. The file is created. There was no names.txt, so the call creates it before adding the lines.
  2. One line per string. one and two are written on separate lines.
  3. Read it back. The array has two items, and index 1 is two.

Example 2 — Append Twice and Keep Everything

Add two lines, then one more. The file keeps all three.

C#
using System;
using System.IO;
using System.Threading.Tasks;

class Program {
    static async Task Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-appendalllinesasync-twice");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string path = Path.Combine(root, "letters.txt");

        await File.AppendAllLinesAsync(path, new[] { "a", "b" });
        await File.AppendAllLinesAsync(path, new[] { "c" });

        string[] lines = await File.ReadAllLinesAsync(path);
        Console.WriteLine(lines.Length);
        Console.WriteLine(string.Join(",", lines));

        Directory.Delete(root, true);
    }
}

How It Works

  1. First append. The file is created with a and b.
  2. Second append. c is added after them. Nothing is replaced.
  3. Order is kept. Reading gives three lines in the order they were added: a,b,c.

Example 3 — Every Appended Line Ends with a Line Break

Check the raw text to see that the method adds the line break for you.

C#
using System;
using System.IO;
using System.Threading.Tasks;

class Program {
    static async Task Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-appendalllinesasync-break");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string path = Path.Combine(root, "log.txt");

        await File.AppendAllLinesAsync(path, new[] { "start" });
        await File.AppendAllLinesAsync(path, new[] { "finish" });

        string text = await File.ReadAllTextAsync(path);
        Console.WriteLine(text.EndsWith(Environment.NewLine));
        Console.WriteLine((await File.ReadAllLinesAsync(path)).Length);

        Directory.Delete(root, true);
    }
}

How It Works

  1. Two separate appends. start and finish are added by two calls.
  2. The break is automatic. Reading the raw text shows it ends with Environment.NewLine, so EndsWith prints True.
  3. No glued lines. Because each call ends with a line break, the second call starts on a fresh line, giving two lines.

Frequently Asked Questions

It adds each string of a sequence to the end of a file, one per line, and closes the file. It creates the file if it is missing and returns a Task.
Yes. Every line, including the last one, is followed by a line break, so you do not add one yourself.
AppendAllLinesAsync keeps the existing content and adds to the end. WriteAllLinesAsync replaces the whole file.
Any IEnumerable<string>, such as a string array, a List<string>, or a LINQ result.
No. The file is created if missing, but the folder must already exist, otherwise you get a DirectoryNotFoundException.

Next: AppendAllText()

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

AppendAllText() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

13 people found this page helpful