C# File AppendAllText() Method

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

Definition

In C#, File.AppendAllText() adds a string to the end of a file in one call. If the file does not exist, it is created first. Old content is never removed.

The method adds no line break for you. If you want each piece on its own line, end each string with Environment.NewLine or \n.

This is a static method of System.IO.File. Call it as File.AppendAllText(...); there is no new File(). For an object that remembers one file, see FileInfo.

Syntax

Pass the path and the text to add.

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

File.AppendAllText(path, "Hello");
File.AppendAllText(path, ", world");

Console.WriteLine(File.ReadAllText(path));
CallWhat it does
File.AppendAllText(path, text)Adds text at the end. Creates the file if needed.
File.AppendAllText(path, text, encoding)Same, with the encoding you choose.
File.AppendAllText(path, line + Environment.NewLine)Adds a full line, ending with a line break.

Every call opens and closes the file, which is fine for a few additions. For many additions in a loop, one File.AppendText writer is faster.

Library declarations
C#
public static void AppendAllText(string path, string? contents);
public static void AppendAllText(string path, string? contents, Encoding encoding);

Parameters

ParameterDescription
pathThe file to append to. Created if missing; its folder must exist.
contentsThe text to add. null or empty adds nothing, but still creates the file.
encodingOptional. Defaults to UTF-8 without BOM.

Return Value

Return ValueDescription
voidNothing is returned. The file now ends with contents.

C# Examples

Example 1 — Append Twice to a New File

The first call creates the file; the second adds to it.

C#
using System;
using System.IO;

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

        string path = Path.Combine(root, "greeting.txt");
        Console.WriteLine(File.Exists(path));

        File.AppendAllText(path, "Hello");
        File.AppendAllText(path, ", world");

        Console.WriteLine(File.ReadAllText(path));

        Directory.Delete(root, true);
    }
}

How It Works

  1. No file at first. The check prints False, so the first append must create the file.
  2. Create by appending. AppendAllText makes the file and writes Hello into it.
  3. Add more. The second call opens the file at its end and adds , world. Reading it shows one joined string.

Example 2 — Append Lines with Environment.NewLine

Add three entries, each ending with a line break, and count the lines.

C#
using System;
using System.IO;

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

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

        File.AppendAllText(path, "one" + Environment.NewLine);
        File.AppendAllText(path, "two" + Environment.NewLine);
        File.AppendAllText(path, "three" + Environment.NewLine);

        string[] lines = File.ReadAllLines(path);
        Console.WriteLine(lines.Length);
        Console.WriteLine(string.Join(",", lines));

        Directory.Delete(root, true);
    }
}

How It Works

  1. The break is part of the text. Each string ends with Environment.NewLine, which is the right break for the current system.
  2. Three calls, three lines. Because each entry ended with a break, ReadAllLines sees three separate lines.
  3. Join to show them. string.Join(",", lines) prints them on one row, so the output looks the same everywhere. For many entries use AppendAllLines, which adds the breaks for you.

Example 3 — The Missing Newline Gotcha

Append without a break and see two pieces join into one line, then fix it.

C#
using System;
using System.IO;

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

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

        File.WriteAllText(path, "a");
        File.AppendAllText(path, "b");
        Console.WriteLine(File.ReadAllText(path));
        Console.WriteLine(File.ReadAllLines(path).Length);

        File.AppendAllText(path, "\nc");
        Console.WriteLine(File.ReadAllLines(path).Length);

        Directory.Delete(root, true);
    }
}

How It Works

  1. Two pieces, one line. The file holds a, and b is added right after it, so the content is ab and there is just 1 line.
  2. Start the new text with a break. Adding "\nc" puts a line break first, so c begins a new line. The file now has 2 lines.
  3. Where to put the break. You can end each entry with a break, or start each one with a break. Pick one habit and keep it, or the file will have blank lines or glued lines.

Frequently Asked Questions

Yes. If the file is missing it is created first, and then the text is written. The folder must already exist.
No. It adds only the text you give. Add Environment.NewLine to the string if you want a line break.
WriteAllText replaces the whole file. AppendAllText keeps the old content and adds the new text at the end.
AppendAllText adds one string as it is. AppendAllLines adds a collection of strings, with a line break after each one.
It works, but it opens and closes the file each time. For many entries, open File.AppendText once and write to the StreamWriter.

Next: AppendText()

Open a StreamWriter that adds text to the end of a file, creating it if needed.

AppendText() tutorial →

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.

8 people found this page helpful