Home C# File Methods AppendAllLinesAsync() C# File AppendAllLinesAsync() Method 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.
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); Call What 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 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 Parameter Description 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 Value Description TaskA task that completes when the lines have been added and the file is closed. There is no result value.
C# Examples 1. Create with lines 2. Append twice 3. Line breaks Example 1 — Append Lines to a New File The file does not exist yet. Appending creates it and writes two lines.
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 The file is created. There was no names.txt, so the call creates it before adding the lines.One line per string. one and two are written on separate lines.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.
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 First append. The file is created with a and b.Second append. c is added after them. Nothing is replaced.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.
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 Two separate appends. start and finish are added by two calls.The break is automatic. Reading the raw text shows it ends with Environment.NewLine, so EndsWith prints True.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 What does File.AppendAllLinesAsync do? 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.
Does AppendAllLinesAsync add a line break after each line? Yes. Every line, including the last one, is followed by a line break, so you do not add one yourself.
What is the difference between AppendAllLinesAsync and WriteAllLinesAsync? AppendAllLinesAsync keeps the existing content and adds to the end. WriteAllLinesAsync replaces the whole file.
What can I pass as the lines? Any IEnumerable<string>, such as a string array, a List<string>, or a LINQ result.
Does AppendAllLinesAsync create the folder? No. The file is created if missing, but the folder must already exist, otherwise you get a DirectoryNotFoundException.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion