Home C# File Methods AppendAllBytesAsync() C# File AppendAllBytesAsync() Method Definition In C#, File.AppendAllBytesAsync() opens a file, adds a byte[] to the end , and closes the file. If the file does not exist, it is created first.
It returns a plain Task. Use await so the next line runs only after the bytes have been added.
This method is new in .NET 9 , together with File.AppendAllBytes(). On older versions of .NET it does not exist, so use a FileStream opened with FileMode.Append instead.
Syntax Pass the path and the bytes to add.
string path = Path.Combine(Path.GetTempPath(), "codetofun-appendallbytesasync.bin");
await File.AppendAllBytesAsync(path, new byte[] { 1, 2, 3 });
await File.AppendAllBytesAsync(path, new byte[] { 4, 5 });
Console.WriteLine(new FileInfo(path).Length); Call What it does await File.AppendAllBytesAsync(path, bytes)Adds the byte array to the end of the file, creating it if needed. await File.AppendAllBytesAsync(path, memory)Adds a ReadOnlyMemory<byte> instead of an array. await File.AppendAllBytesAsync(path, bytes, token)Stops with an OperationCanceledException if the token is cancelled.
Use File.WriteAllBytesAsync when you want to replace the file instead of adding to it.
Library declarations public static Task AppendAllBytesAsync(string path, byte[] bytes, CancellationToken cancellationToken = default);
public static Task AppendAllBytesAsync(string path, ReadOnlyMemory<byte> bytes, CancellationToken cancellationToken = default); Parameters Parameter Description pathThe file to add to. It is created if it does not exist. bytesThe data to add, as a byte[] or a ReadOnlyMemory<byte>. cancellationTokenOptional. A token that lets you cancel the operation before it finishes.
Return Value Return Value Description TaskA task that completes when the bytes have been added and the file is closed. There is no result value.
Important
Needs .NET 9 or later. If the compiler says File does not contain AppendAllBytesAsync, check your target framework. Await each append before starting the next, because two tasks writing to the same file can fail with IOException.
C# Examples 1. Create with bytes 2. Append twice 3. Append text bytes Example 1 — Append Bytes to a New File The file does not exist yet. Appending creates it.
using System;
using System.IO;
using System.Threading.Tasks;
class Program {
static async Task Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-appendallbytesasync");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "data.bin");
Console.WriteLine(File.Exists(path));
await File.AppendAllBytesAsync(path, new byte[] { 1, 2, 3 });
Console.WriteLine(File.Exists(path));
Console.WriteLine(new FileInfo(path).Length);
Directory.Delete(root, true);
}
} How It Works No file yet. File.Exists prints False before the append.Append creates it. The call creates data.bin and writes the three bytes.Check the size. The file exists and is three bytes long.Example 2 — Append Twice and Keep Both Parts Add three bytes, then two more, and read all of them.
using System;
using System.IO;
using System.Threading.Tasks;
class Program {
static async Task Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-appendallbytesasync-twice");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "parts.bin");
await File.AppendAllBytesAsync(path, new byte[] { 1, 2, 3 });
await File.AppendAllBytesAsync(path, new byte[] { 4, 5 });
byte[] bytes = await File.ReadAllBytesAsync(path);
Console.WriteLine(bytes.Length);
Console.WriteLine(string.Join(",", bytes));
Directory.Delete(root, true);
}
} How It Works First append. The file starts with 1, 2, 3.Second append. 4, 5 are added after them. Nothing is replaced.Order is kept. Reading gives five bytes in the order they were added.Example 3 — Append Text as Bytes Turn two pieces of text into bytes and append them. Reading the file as text shows the joined result.
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
class Program {
static async Task Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-appendallbytesasync-text");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "greeting.txt");
await File.AppendAllBytesAsync(path, Encoding.UTF8.GetBytes("Hello"));
await File.AppendAllBytesAsync(path, Encoding.UTF8.GetBytes(" World"));
Console.WriteLine(await File.ReadAllTextAsync(path));
Console.WriteLine(new FileInfo(path).Length);
Directory.Delete(root, true);
}
} How It Works Text to bytes. Encoding.UTF8.GetBytes turns each piece into a byte array.Two appends. Hello is five bytes and World is six bytes, added one after the other.A normal text file. The bytes are plain UTF-8, so ReadAllTextAsync returns Hello World, and the size is eleven bytes.Frequently Asked Questions What does File.AppendAllBytesAsync do? It adds a byte array to the end of a file and closes the file. If the file does not exist, it creates it. It returns a Task.
Which .NET version has File.AppendAllBytesAsync? .NET 9 and later. It does not exist in .NET 8 or older, where you can use a FileStream opened with FileMode.Append.
What is the difference between AppendAllBytesAsync and WriteAllBytesAsync? AppendAllBytesAsync keeps the existing bytes and adds to the end. WriteAllBytesAsync replaces the whole file.
Can I pass ReadOnlyMemory<byte>? Yes. There is an overload for ReadOnlyMemory<byte>, which lets you append only part of a buffer.
Does AppendAllBytesAsync 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