In C#, File.WriteAllText() saves a string into a file in one call. It creates the file when it is missing, replaces the content when it already exists, and closes the file when it is done.
The default encoding is UTF-8 without a byte order mark. The method does not create folders, so the folder in the path must already exist.
This is a static method of System.IO.File. Call it as File.WriteAllText(...); there is no new File(). For an object that remembers one file, see FileInfo.
Syntax
Give the path and the text. Add an Encoding if you need something other than UTF-8.
Creates or overwrites the file with text, saved as UTF-8 without BOM.
File.WriteAllText(path, text, encoding)
Same, using the Encoding you pass, such as Encoding.Unicode.
File.WriteAllText(path, "")
Leaves an empty file. It is a short way to clear a file.
WriteAllText is a one-shot save. To write in many small steps, use a StreamWriter from File.CreateText. To add to the end, use File.AppendAllText.
Library declarations
C#
public static void WriteAllText(string path, string? contents);
public static void WriteAllText(string path, string? contents, Encoding encoding);
Parameters
Parameter
Description
path
The file to write. It is created if missing, and its folder must exist.
contents
The text to save. null or an empty string creates an empty file.
encoding
Optional. The Encoding used to turn the text into bytes. Defaults to UTF-8 without BOM.
Return Value
Return Value
Description
void
Nothing is returned. After a normal return the file holds exactly contents.
C# Examples
Example 1 — Save a Message and Read It Back
The first program for any file API: write something, then read it.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-writealltext");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "hello.txt");
Console.WriteLine(File.Exists(path));
File.WriteAllText(path, "Hello, file!");
Console.WriteLine(File.Exists(path));
Console.WriteLine(File.ReadAllText(path));
Directory.Delete(root, true);
}
}
Output
False
True
Hello, file!
How It Works
Start without a file. The demo folder is new, so the first Exists check prints False.
One call saves it.WriteAllText creates hello.txt and puts the string inside. The file is closed again when the call returns.
Verify.Exists now prints True, and ReadAllText returns the same message.
Example 2 — A Second Write Replaces the First
Write twice to the same file and check what is left.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-writealltext-over");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "status.txt");
File.WriteAllText(path, "first");
Console.WriteLine(File.ReadAllText(path));
File.WriteAllText(path, "second");
Console.WriteLine(File.ReadAllText(path));
Console.WriteLine(File.ReadAllText(path).Length);
Directory.Delete(root, true);
}
}
Output
first
second
6
How It Works
First write. The file is created and holds first.
Second write replaces.WriteAllText does not add to the file. The old text is gone and only second remains.
No leftovers. The length is 6, the size of second, so no part of the shorter old text is left behind. Use AppendAllText if you want to keep the old text.
Example 3 — Handle a Missing Folder
See the exception for a folder that does not exist, then create the folder and save again.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-writealltext-dir");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "reports", "today.txt");
try {
File.WriteAllText(path, "hi");
} catch (DirectoryNotFoundException) {
Console.WriteLine("Folder is missing");
}
Directory.CreateDirectory(Path.GetDirectoryName(path));
File.WriteAllText(path, "hi");
Console.WriteLine("Saved: " + File.ReadAllText(path));
Directory.Delete(root, true);
}
}
Output
Folder is missing
Saved: hi
How It Works
A path with a new folder. The file would live in reports, but that folder was never created.
No automatic folders.WriteAllText only creates the file, so the first call throws DirectoryNotFoundException and the catch block prints a message.
Create the folder from the path.Path.GetDirectoryName(path) returns the folder part and CreateDirectory builds it. The second save works.
A habit worth keeping. Calling CreateDirectory before every save is safe, because it does nothing when the folder already exists.
Frequently Asked Questions
Yes. If the file exists, its content is replaced by the new text. Use File.AppendAllText to add to the end of the file instead.
No. It creates only the file. If the folder is missing you get DirectoryNotFoundException, so call Directory.CreateDirectory first.
UTF-8 without a byte order mark. Pass an Encoding as the third argument to use another one, such as Encoding.Unicode or new UTF8Encoding(true) for UTF-8 with BOM.
Join the lines with Environment.NewLine in one string, or use File.WriteAllLines, which accepts an array or a sequence of lines.
Call File.WriteAllText(path, "") to leave an empty file. The file stays on disk with a length of zero.