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.
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
Parameter
Description
path
The file to read. It must exist.
cancellationToken
Optional. A token that lets you cancel the read before it finishes.
Return Value
Return Value
Description
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);
}
}
Output
5
72
Hello
How It Works
Five known bytes. The values 72, 101, 108, 108, 111 are the ASCII codes of the letters in Hello.
Read them all.ReadAllBytesAsync returns an array with five bytes, and the first one is 72.
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);
}
}
Output
3
72 105 33
How It Works
A text file is bytes.WriteAllTextAsync saves the text as UTF-8 with no BOM.
One byte per letter.H, i, and ! are plain ASCII, so the file has three bytes.
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);
}
}
Output
0
FileNotFoundException
How It Works
Create an empty file. Writing an empty byte array leaves a file with no content.
Empty is not an error. Reading it returns an array of length 0.
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.