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.
The file to add to. It is created if it does not exist.
contents
The text to add to the end of the file.
encoding
Optional. The Encoding used to turn the text into bytes. Defaults to UTF-8 without a BOM.
cancellationToken
Optional. A token that lets you cancel the operation before it finishes.
Return Value
Return Value
Description
Task
A 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);
}
}
Output
False
Hello World
True
How It Works
No file yet.File.Exists prints False before any append.
Append creates the file. The first call creates greeting.txt with Hello. The second call adds World after it.
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);
}
}
Output
3
Entry 3
How It Works
A loop of appends. Each pass adds Entry 1, Entry 2, then Entry 3. Each call is awaited before the next one starts.
The line break matters.Environment.NewLine ends every entry, so the file has three separate lines.
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);
}
}
Output
abcdef
xyz
How It Works
Start with abc.WriteAllTextAsync creates the file with three letters.
Append keeps the old text.AppendAllTextAsync adds def to the end, so the file reads abcdef.
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.