C# File ReadAllTextAsync() Method

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

Definition

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.

C#
string path = Path.Combine(Path.GetTempPath(), "codetofun-readalltextasync.txt");
await File.WriteAllTextAsync(path, "Hello, async!");

string text = await File.ReadAllTextAsync(path);

Console.WriteLine(text);
CallWhat it does
await File.ReadAllTextAsync(path)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

ParameterDescription
pathThe file to read. It must exist.
encodingOptional. The Encoding used to decode the bytes. Defaults to UTF-8, with BOM detection.
cancellationTokenOptional. A token that lets you cancel the read before it finishes.

Return Value

Return ValueDescription
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);
    }
}

How It Works

  1. A known file. WriteAllTextAsync saves Hello, async!, so the example does not depend on files on your computer.
  2. One await, one string. ReadAllTextAsync reads every character, closes the file, and the await hands you the text.
  3. 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);
    }
}

How It Works

  1. 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.
  2. Name the encoding. ReadAllTextAsync(path, Encoding.Unicode) decodes the bytes as UTF-16 and returns cafe.
  3. 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);
    }
}

How It Works

  1. A token source. CancellationTokenSource creates a token you can hand to any async method that accepts one.
  2. First read works. While the token is not cancelled, ReadAllTextAsync returns Report ready as usual.
  3. 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.

Next: ReadLines()

Read the lines of a text file lazily, one at a time, with IEnumerable.

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