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.
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
Parameter
Description
path
The file to append to. Created if missing; its folder must exist.
contents
The text to add. null or empty adds nothing, but still creates the file.
encoding
Optional. Defaults to UTF-8 without BOM.
Return Value
Return Value
Description
void
Nothing 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);
}
}
Output
False
Hello, world
How It Works
No file at first. The check prints False, so the first append must create the file.
Create by appending.AppendAllText makes the file and writes Hello into it.
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);
}
}
Output
3
one,two,three
How It Works
The break is part of the text. Each string ends with Environment.NewLine, which is the right break for the current system.
Three calls, three lines. Because each entry ended with a break, ReadAllLines sees three separate lines.
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);
}
}
Output
ab
1
2
How It Works
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.
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.
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.