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.
The file to write. It is created if missing and replaced if it exists.
contents
The text to save. A null or empty string gives an empty file.
encoding
Optional. The Encoding used to turn the text into bytes. Defaults to UTF-8 without a BOM.
cancellationToken
Optional. A token that lets you cancel the write before it finishes.
Return Value
Return Value
Description
Task
A 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);
}
}
Output
Saved with async
True
How It Works
Await the write. The line with await finishes only after the text is saved and the file is closed.
Read it back.ReadAllTextAsync returns exactly the string that was written.
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);
}
}
Output
second
6
How It Works
Two writes, one path. The first call creates the file with first version.
The second call replaces it.WriteAllTextAsync does not append. It empties the file and writes the new text.
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);
}
}
Output
UTF-8, no BOM: 4
UTF-8, BOM: 7
UTF-16: 10
How It Works
Same word, three files. Each call saves cafe with a different Encoding.
UTF-8 sizes. Plain UTF-8 uses one byte per letter, so four. Encoding.UTF8 also writes a three byte BOM, so seven.
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.