C# File AppendAllTextAsync() Method

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

Definition

In C#, File.AppendAllTextAsync() opens a file, adds a string to the end, and closes the file. If the file does not exist, it is created first. It is the async version of File.AppendAllText().

It returns a plain Task. Use await so the next line runs only after the text has been added.

The method adds exactly the text you give it. It does not add a line break by itself, so add Environment.NewLine when you want one entry per line.

Syntax

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

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

await File.AppendAllTextAsync(path, "Hello");
await File.AppendAllTextAsync(path, " World");

Console.WriteLine(await File.ReadAllTextAsync(path));
CallWhat it does
await File.AppendAllTextAsync(path, text)Adds the text to the end of the file as UTF-8, creating the file if needed.
await File.AppendAllTextAsync(path, text, Encoding.Unicode)Adds the text using the encoding you name.
await File.AppendAllTextAsync(path, text + Environment.NewLine)Adds the text followed by a line break, one entry per line.

For many small appends in a row, keep one StreamWriter open instead. Each call to AppendAllTextAsync opens and closes the file again.

Library declarations
C#
public static Task AppendAllTextAsync(string path, string? contents, CancellationToken cancellationToken = default);
public static Task AppendAllTextAsync(string path, string? contents, Encoding encoding, CancellationToken cancellationToken = default);

Parameters

ParameterDescription
pathThe file to add to. It is created if it does not exist.
contentsThe text to add to the end of the file.
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 text has been added and the file is closed. There is no result value.

C# Examples

Example 1 — Append to a File That Does Not Exist Yet

The first call creates the file. The second call adds to it.

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

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

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

        Console.WriteLine(File.Exists(path));

        await File.AppendAllTextAsync(path, "Hello");
        await File.AppendAllTextAsync(path, " World");

        Console.WriteLine(await File.ReadAllTextAsync(path));
        Console.WriteLine(File.Exists(path));

        Directory.Delete(root, true);
    }
}

How It Works

  1. No file yet. File.Exists prints False before any append.
  2. Append creates the file. The first call creates greeting.txt with Hello. The second call adds World after it.
  3. Read the result. The file now holds Hello World, and File.Exists prints True.

Example 2 — Write One Log Entry per Line

Add three entries in a loop, each with a line break, and read the file back as 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-appendalltextasync-log");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

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

        for (int i = 1; i <= 3; i++) {
            await File.AppendAllTextAsync(path, "Entry " + i + Environment.NewLine);
        }

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

        Directory.Delete(root, true);
    }
}

How It Works

  1. A loop of appends. Each pass adds Entry 1, Entry 2, then Entry 3. Each call is awaited before the next one starts.
  2. The line break matters. Environment.NewLine ends every entry, so the file has three separate lines.
  3. Read it as lines. ReadAllLinesAsync returns an array of three strings, and index 2 is the last entry.

Example 3 — Compare Appending with Writing

Append keeps what is already there. A write throws it away.

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

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

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

        await File.WriteAllTextAsync(path, "abc");
        await File.AppendAllTextAsync(path, "def");
        Console.WriteLine(await File.ReadAllTextAsync(path));

        await File.WriteAllTextAsync(path, "xyz");
        Console.WriteLine(await File.ReadAllTextAsync(path));

        Directory.Delete(root, true);
    }
}

How It Works

  1. Start with abc. WriteAllTextAsync creates the file with three letters.
  2. Append keeps the old text. AppendAllTextAsync adds def to the end, so the file reads abcdef.
  3. Write replaces everything. The last WriteAllTextAsync empties the file first, so only xyz is left.

Frequently Asked Questions

It adds a string to the end of a file and closes the file. If the file does not exist, it creates it. It returns a Task that completes when the text is written.
No. It adds exactly the text you pass. Add Environment.NewLine to the text if you want each entry on its own line.
AppendAllTextAsync keeps the existing content and adds to the end. WriteAllTextAsync replaces the whole file.
Not safely on the same file. Two tasks opening the file together can cause an IOException. Await each append, or use one StreamWriter for many writes.
No. The file is created if missing, but the folder must already exist, otherwise you get a DirectoryNotFoundException.

Next: AppendText()

Open a StreamWriter that adds text to the end of a file, creating it if needed.

AppendText() 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