C# File AppendAllBytesAsync() Method

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

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.

C#
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);
CallWhat 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
C#
public static Task AppendAllBytesAsync(string path, byte[] bytes, CancellationToken cancellationToken = default);
public static Task AppendAllBytesAsync(string path, ReadOnlyMemory<byte> bytes, CancellationToken cancellationToken = default);

Parameters

ParameterDescription
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 ValueDescription
TaskA task that completes when the bytes have been added and the file is closed. There is no result value.

C# Examples

Example 1 — Append Bytes to a New File

The file does not exist yet. Appending creates 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-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

  1. No file yet. File.Exists prints False before the append.
  2. Append creates it. The call creates data.bin and writes the three bytes.
  3. 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.

C#
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

  1. First append. The file starts with 1, 2, 3.
  2. Second append. 4, 5 are added after them. Nothing is replaced.
  3. 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.

C#
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

  1. Text to bytes. Encoding.UTF8.GetBytes turns each piece into a byte array.
  2. Two appends. Hello is five bytes and World is six bytes, added one after the other.
  3. A normal text file. The bytes are plain UTF-8, so ReadAllTextAsync returns Hello World, and the size is eleven bytes.

Frequently Asked Questions

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.
.NET 9 and later. It does not exist in .NET 8 or older, where you can use a FileStream opened with FileMode.Append.
AppendAllBytesAsync keeps the existing bytes and adds to the end. WriteAllBytesAsync replaces the whole file.
Yes. There is an overload for ReadOnlyMemory<byte>, which lets you append only part of a buffer.
No. The file is created if missing, but the folder must already exist, otherwise you get a DirectoryNotFoundException.

Next: AppendAllLines()

Add lines to the end of a file, each followed by a line break.

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