C# File WriteAllText() Method

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

Definition

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.

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

File.WriteAllText(path, "Hello, file!");

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

ParameterDescription
pathThe file to write. It is created if missing, and its folder must exist.
contentsThe text to save. null or an empty string creates an empty file.
encodingOptional. The Encoding used to turn the text into bytes. Defaults to UTF-8 without BOM.

Return Value

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

How It Works

  1. Start without a file. The demo folder is new, so the first Exists check prints False.
  2. One call saves it. WriteAllText creates hello.txt and puts the string inside. The file is closed again when the call returns.
  3. 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);
    }
}

How It Works

  1. First write. The file is created and holds first.
  2. Second write replaces. WriteAllText does not add to the file. The old text is gone and only second remains.
  3. 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);
    }
}

How It Works

  1. A path with a new folder. The file would live in reports, but that folder was never created.
  2. No automatic folders. WriteAllText only creates the file, so the first call throws DirectoryNotFoundException and the catch block prints a message.
  3. Create the folder from the path. Path.GetDirectoryName(path) returns the folder part and CreateDirectory builds it. The second save works.
  4. 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.

Back to the method index

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

Method index →

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.

7 people found this page helpful