In C#, File.ReadAllTextAsync() opens a text file, reads everything in it into a single string, and closes the file. It is the async version of File.ReadAllText().
It returns a Task<string>. Put await in front of the call and C# gives you the text when the disk is done, while the calling thread is free to do other work in the meantime.
This is a static method of System.IO.File, so call it as File.ReadAllTextAsync(...) from an async method such as static async Task Main().
Syntax
Pass the path. Add an Encoding when the file is not UTF-8, and a CancellationToken if you want to be able to stop.
Returns the whole file as a string, decoded as UTF-8 (or by BOM).
await File.ReadAllTextAsync(path, Encoding.ASCII)
Decodes with the encoding you name.
await File.ReadAllTextAsync(path, token)
Stops with an OperationCanceledException if the token is cancelled.
ReadAllTextAsync still loads the whole file into memory. For a very large file use File.ReadLinesAsync or a StreamReader and read it in pieces.
Library declarations
C#
public static Task<string> ReadAllTextAsync(string path, CancellationToken cancellationToken = default);
public static Task<string> ReadAllTextAsync(string path, Encoding encoding, CancellationToken cancellationToken = default);
Parameters
Parameter
Description
path
The file to read. It must exist.
encoding
Optional. The Encoding used to decode the bytes. Defaults to UTF-8, with BOM detection.
cancellationToken
Optional. A token that lets you cancel the read before it finishes.
Return Value
Return Value
Description
Task<string>
A task that gives the full content of the file once awaited. An empty file gives an empty string.
C# Examples
Example 1 — Read a Whole File into a String
Save a short message, read it back with await, and check the length.
C#
using System;
using System.IO;
using System.Threading.Tasks;
class Program {
static async Task Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-readalltextasync");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "message.txt");
await File.WriteAllTextAsync(path, "Hello, async!");
string text = await File.ReadAllTextAsync(path);
Console.WriteLine(text);
Console.WriteLine(text.Length);
Directory.Delete(root, true);
}
}
Output
Hello, async!
13
How It Works
A known file.WriteAllTextAsync saves Hello, async!, so the example does not depend on files on your computer.
One await, one string.ReadAllTextAsync reads every character, closes the file, and the await hands you the text.
Count the characters. The message has thirteen characters, including the comma, the space, and the exclamation mark, so Length prints 13.
Example 2 — Read a UTF-16 File with and Without an Encoding
Save a word as UTF-16 and read it back two ways: by naming the encoding, and by letting the BOM decide.
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-readalltextasync-enc");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "utf16.txt");
await File.WriteAllTextAsync(path, "cafe", Encoding.Unicode);
byte[] bytes = await File.ReadAllBytesAsync(path);
string named = await File.ReadAllTextAsync(path, Encoding.Unicode);
string detected = await File.ReadAllTextAsync(path);
Console.WriteLine(bytes.Length);
Console.WriteLine(named);
Console.WriteLine(named == detected);
Directory.Delete(root, true);
}
}
Output
10
cafe
True
How It Works
UTF-16 on disk.Encoding.Unicode stores each letter in two bytes and adds a two byte BOM at the start, so four letters become ten bytes.
Name the encoding.ReadAllTextAsync(path, Encoding.Unicode) decodes the bytes as UTF-16 and returns cafe.
Let the BOM decide. Without an encoding the method sees the BOM and switches to UTF-16 by itself, so both strings match and the comparison prints True.
Example 3 — Cancel a Read with a CancellationToken
Pass a token to the method. Cancel it, and the next read stops with an OperationCanceledException.
C#
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
class Program {
static async Task Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-readalltextasync-cancel");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "report.txt");
await File.WriteAllTextAsync(path, "Report ready");
using CancellationTokenSource cts = new CancellationTokenSource();
string first = await File.ReadAllTextAsync(path, cts.Token);
Console.WriteLine(first);
cts.Cancel();
try {
await File.ReadAllTextAsync(path, cts.Token);
Console.WriteLine("Read finished");
} catch (OperationCanceledException) {
Console.WriteLine("Cancelled before reading");
}
Directory.Delete(root, true);
}
}
Output
Report ready
Cancelled before reading
How It Works
A token source.CancellationTokenSource creates a token you can hand to any async method that accepts one.
First read works. While the token is not cancelled, ReadAllTextAsync returns Report ready as usual.
Cancel, then read. After cts.Cancel() the next call is cancelled straight away. The await throws OperationCanceledException, the catch prints a message, and Read finished is never reached.
Frequently Asked Questions
A Task<string>. When you await it you get the whole file as one string. Without await you only hold the task, not the text.
ReadAllText blocks the thread until the disk is finished. The async version lets the thread do other work while it waits, which keeps desktop and web apps responsive. For a tiny console demo both give the same result.
UTF-8 by default, and it detects UTF-16 and UTF-32 files that start with a byte order mark. Pass an Encoding as the second argument for other cases.
Pass a CancellationToken as the last argument. If the token is cancelled, awaiting the task throws an OperationCanceledException that you can catch.
FileNotFoundException, and DirectoryNotFoundException for a missing folder. Because the method is async, the exception appears when you await the task, so put the await inside try/catch.