C# File WriteAllTextAsync() Method

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

Definition

In C#, File.WriteAllTextAsync() creates a file, writes a string into it, and closes the file. If the file already exists, its old content is replaced. It is the async version of File.WriteAllText().

It returns a plain Task, so there is no value to read. Use await to wait until the text is on disk before you do the next step.

This is a static method of System.IO.File. The folder must already exist; use Directory.CreateDirectory first if it does not.

Syntax

Pass the path and the text. Add an Encoding to save in something other than UTF-8.

C#
string path = Path.Combine(Path.GetTempPath(), "codetofun-writealltextasync.txt");

await File.WriteAllTextAsync(path, "Hello, async!");

Console.WriteLine(File.Exists(path));
CallWhat it does
await File.WriteAllTextAsync(path, text)Saves the text as UTF-8 without a BOM, creating or replacing the file.
await File.WriteAllTextAsync(path, text, Encoding.Unicode)Saves with the encoding you name, including its BOM if it has one.
await File.WriteAllTextAsync(path, text, token)Stops with an OperationCanceledException if the token is cancelled.

Use File.AppendAllTextAsync when you want to keep what is already in the file and add to the end.

Library declarations
C#
public static Task WriteAllTextAsync(string path, string? contents, CancellationToken cancellationToken = default);
public static Task WriteAllTextAsync(string path, string? contents, Encoding encoding, CancellationToken cancellationToken = default);

Parameters

ParameterDescription
pathThe file to write. It is created if missing and replaced if it exists.
contentsThe text to save. A null or empty string gives an empty file.
encodingOptional. The Encoding used to turn the text into bytes. Defaults to UTF-8 without a BOM.
cancellationTokenOptional. A token that lets you cancel the write before it finishes.

Return Value

Return ValueDescription
TaskA task that completes when the text has been written and the file is closed. There is no result value.

C# Examples

Example 1 — Save a String and Read It Back

Write a short message, check that the file exists, and read it back.

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

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

        string path = Path.Combine(root, "note.txt");

        await File.WriteAllTextAsync(path, "Saved with async");

        Console.WriteLine(await File.ReadAllTextAsync(path));
        Console.WriteLine(File.Exists(path));

        Directory.Delete(root, true);
    }
}

How It Works

  1. Await the write. The line with await finishes only after the text is saved and the file is closed.
  2. Read it back. ReadAllTextAsync returns exactly the string that was written.
  3. Check the file. File.Exists prints True because the write created the file.

Example 2 — Write Again to Replace the Content

Write twice to the same path and see that only the second text is left.

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

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

        string path = Path.Combine(root, "version.txt");

        await File.WriteAllTextAsync(path, "first version");
        await File.WriteAllTextAsync(path, "second");

        string text = await File.ReadAllTextAsync(path);
        Console.WriteLine(text);
        Console.WriteLine(text.Length);

        Directory.Delete(root, true);
    }
}

How It Works

  1. Two writes, one path. The first call creates the file with first version.
  2. The second call replaces it. WriteAllTextAsync does not append. It empties the file and writes the new text.
  3. Only the new text remains. Reading shows second, which has six characters.

Example 3 — See How the Encoding Changes the File Size

Save the same word with three encodings and compare the number of bytes on disk.

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

        string plain = Path.Combine(root, "utf8-plain.txt");
        string withBom = Path.Combine(root, "utf8-bom.txt");
        string utf16 = Path.Combine(root, "utf16.txt");

        await File.WriteAllTextAsync(plain, "cafe", new UTF8Encoding(false));
        await File.WriteAllTextAsync(withBom, "cafe", Encoding.UTF8);
        await File.WriteAllTextAsync(utf16, "cafe", Encoding.Unicode);

        Console.WriteLine("UTF-8, no BOM: " + (await File.ReadAllBytesAsync(plain)).Length);
        Console.WriteLine("UTF-8, BOM: " + (await File.ReadAllBytesAsync(withBom)).Length);
        Console.WriteLine("UTF-16: " + (await File.ReadAllBytesAsync(utf16)).Length);

        Directory.Delete(root, true);
    }
}

How It Works

  1. Same word, three files. Each call saves cafe with a different Encoding.
  2. UTF-8 sizes. Plain UTF-8 uses one byte per letter, so four. Encoding.UTF8 also writes a three byte BOM, so seven.
  3. UTF-16 size. Two bytes per letter plus a two byte BOM make ten. Leave the encoding out and you get the plain UTF-8 size of four.

Frequently Asked Questions

It creates a file, writes a string into it, and closes it. If the file already exists, the old content is replaced. It returns a Task that completes when the data is written.
Yes. It replaces the whole content. Use File.AppendAllTextAsync when you want to add to the end instead.
UTF-8 without a byte order mark by default. Pass an Encoding such as Encoding.Unicode or Encoding.UTF8 to change it. Note that Encoding.UTF8 writes a BOM, while new UTF8Encoding(false) does not.
No. The folder must exist. Call Directory.CreateDirectory first, otherwise you get a DirectoryNotFoundException.
Yes, in almost every case. Without await your code moves on before the write finishes, and any error is silently lost.

Back to the method index

See all 48 File method guides grouped by what they do.

Method index →

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