C# File ReadAllBytesAsync() Method

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

Definition

In C#, File.ReadAllBytesAsync() opens a file, reads all of its bytes into a byte[], and closes the file. It is the async version of File.ReadAllBytes().

It returns a Task<byte[]>. After you await it, you can look at single bytes, count them, or turn them into text with an Encoding.

Use it for binary files such as images, or when you want to see exactly what is stored on disk.

Syntax

Pass the path. The result is the file exactly as it is on disk, with no decoding.

C#
string path = Path.Combine(Path.GetTempPath(), "codetofun-readallbytesasync.bin");
await File.WriteAllBytesAsync(path, new byte[] { 72, 105 });

byte[] bytes = await File.ReadAllBytesAsync(path);

Console.WriteLine(bytes.Length);
CallWhat it does
await File.ReadAllBytesAsync(path)Returns every byte of the file as a byte[].
await File.ReadAllBytesAsync(path, token)Stops with an OperationCanceledException if the token is cancelled.
Encoding.UTF8.GetString(bytes)Turns the bytes into a string after you have read them.

The whole file is loaded into memory. For a very large file, read it in pieces with a FileStream instead.

Library declarations
C#
public static Task<byte[]> ReadAllBytesAsync(string path, CancellationToken cancellationToken = default);

Parameters

ParameterDescription
pathThe file to read. It must exist.
cancellationTokenOptional. A token that lets you cancel the read before it finishes.

Return Value

Return ValueDescription
Task<byte[]>A task that gives the bytes of the file once awaited. An empty file gives an array of length 0.

C# Examples

Example 1 — Read the Bytes of a Small File

Save five known bytes, read them back, and turn them into text.

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-readallbytesasync");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string path = Path.Combine(root, "data.bin");
        await File.WriteAllBytesAsync(path, new byte[] { 72, 101, 108, 108, 111 });

        byte[] bytes = await File.ReadAllBytesAsync(path);

        Console.WriteLine(bytes.Length);
        Console.WriteLine(bytes[0]);
        Console.WriteLine(Encoding.UTF8.GetString(bytes));

        Directory.Delete(root, true);
    }
}

How It Works

  1. Five known bytes. The values 72, 101, 108, 108, 111 are the ASCII codes of the letters in Hello.
  2. Read them all. ReadAllBytesAsync returns an array with five bytes, and the first one is 72.
  3. Decode to text. Encoding.UTF8.GetString turns those bytes into Hello.

Example 2 — See the Bytes Behind a Text File

Save the text Hi! and look at the bytes that end up on disk.

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

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

        string path = Path.Combine(root, "hi.txt");
        await File.WriteAllTextAsync(path, "Hi!");

        byte[] bytes = await File.ReadAllBytesAsync(path);

        Console.WriteLine(bytes.Length);
        Console.WriteLine(string.Join(" ", bytes));

        Directory.Delete(root, true);
    }
}

How It Works

  1. A text file is bytes. WriteAllTextAsync saves the text as UTF-8 with no BOM.
  2. One byte per letter. H, i, and ! are plain ASCII, so the file has three bytes.
  3. The codes. string.Join prints 72 105 33, the ASCII codes of those three characters.

Example 3 — An Empty File and a Missing File

An empty file gives an empty array. A missing file throws an exception that you can catch.

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

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

        string empty = Path.Combine(root, "empty.bin");
        string missing = Path.Combine(root, "missing.bin");

        await File.WriteAllBytesAsync(empty, Array.Empty<byte>());

        byte[] bytes = await File.ReadAllBytesAsync(empty);
        Console.WriteLine(bytes.Length);

        try {
            await File.ReadAllBytesAsync(missing);
        } catch (FileNotFoundException ex) {
            Console.WriteLine(ex.GetType().Name);
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. Create an empty file. Writing an empty byte array leaves a file with no content.
  2. Empty is not an error. Reading it returns an array of length 0.
  3. Missing is an error. The second read throws FileNotFoundException when awaited, and catch prints the exception name.

Frequently Asked Questions

A Task<byte[]>. After you await it, you get every byte of the file in an array.
Use ReadAllBytesAsync for binary data such as images and archives, or when you want the exact bytes on disk. Use ReadAllTextAsync when the file holds text.
Pass the array to an Encoding, for example Encoding.UTF8.GetString(bytes). Pick the same encoding that was used to save the text.
Not really, because the whole file is loaded into memory. For big files, read in pieces with a FileStream.
A FileNotFoundException is thrown when you await the task. Put the await inside try/catch or check File.Exists first.

Next: ReadAllLines()

Read every line of a text file into a string array.

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