In C#, File.AppendText() returns a StreamWriter that writes at the end of a text file. If the file does not exist, it is created. Old content is never removed.
It is a good fit for log files, where you add a few lines at a time. The text is saved as UTF-8. Wrap the writer in a using block so the data is flushed and the file is released.
This is a static method of System.IO.File. Call it as File.AppendText(...); there is no new File(). For an object that remembers one file, see FileInfo.
Syntax
Open the writer, write what you need, and let using close it.
Opens or creates the file and returns a StreamWriter positioned at the end.
writer.WriteLine(text)
Adds the text followed by a line break.
writer.Write(text)
Adds the text with no line break.
For one quick addition, File.AppendAllText does the same in a single call. Choose AppendText when you write many pieces in one go, because the file is opened only once.
Library declarations
C#
public static StreamWriter AppendText(string path);
Parameters
Parameter
Description
path
The file to append to. It is created if it does not exist, but its folder must exist.
Return Value
Return Value
Description
StreamWriter
A UTF-8 writer at the end of the file. Dispose it to flush the text and close the file.
C# Examples
Example 1 — Append to a File That Does Not Exist Yet
The first AppendText 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-appendtext");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "log.txt");
Console.WriteLine(File.Exists(path));
using (StreamWriter writer = File.AppendText(path)) {
writer.WriteLine("start");
}
using (StreamWriter writer = File.AppendText(path)) {
writer.WriteLine("next");
}
Console.WriteLine(string.Join("|", File.ReadAllLines(path)));
Directory.Delete(root, true);
}
}
Output
False
start|next
How It Works
The file is new. The first Exists prints False, because nothing has been written yet.
The first call creates it.AppendText makes the missing file and writes start at the end of an empty file.
The second call extends it. A new writer opens at the end, so next lands after start instead of replacing it.
Join for a tidy print.string.Join("|", lines) shows both lines on one row, so the output does not depend on the system line break.
Example 2 — A Reusable Log Method
Put AppendText in a helper and call it three times.
C#
using System;
using System.IO;
class Program {
static void Log(string path, string message) {
using (StreamWriter writer = File.AppendText(path)) {
writer.WriteLine(message);
}
}
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-appendtext-log");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "app.log");
Log(path, "first");
Log(path, "second");
Log(path, "third");
string[] lines = File.ReadAllLines(path);
Console.WriteLine(lines.Length);
Console.WriteLine(lines[lines.Length - 1]);
Directory.Delete(root, true);
}
}
Output
3
third
How It Works
One small helper.Log opens the writer, writes one line, and disposes it, so a crash later never leaves the log file locked.
Each call adds a line. Three calls give three lines in the order they were made. The file is created by the first call.
Read the end.lines[lines.Length - 1] is the last entry, third. In a real log you would also add a time stamp.
Example 3 — Write Does Not Add a Line Break
Append to a file that has no final newline and see how the pieces join.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-appendtext-write");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "line.txt");
File.WriteAllText(path, "start");
using (StreamWriter writer = File.AppendText(path)) {
writer.Write("-more");
writer.Write("-again");
writer.WriteLine();
writer.WriteLine("next line");
}
string[] lines = File.ReadAllLines(path);
Console.WriteLine(lines.Length);
Console.WriteLine(lines[0]);
Console.WriteLine(lines[1]);
Directory.Delete(root, true);
}
}
Output
2
start-more-again
next line
How It Works
The old file has no newline.WriteAllText saved start only, so the last line is open.
Write continues the line. Both Write calls extend that same line, so it grows to start-more-again.
An empty WriteLine ends it.writer.WriteLine() with no argument adds only a line break. The next WriteLine then starts a new line.
Two lines in total.ReadAllLines returns the joined line and next line.
Frequently Asked Questions
Yes. The file is created when it does not exist. The folder must already exist, otherwise DirectoryNotFoundException is thrown.
AppendAllText adds one string in a single call and closes the file. AppendText gives you a StreamWriter for many writes with one open and close.
No. It writes exactly what you give it. If the file does not end with a line break, add one yourself or the new text joins the old last line.
The writer buffers text and keeps the file locked. Disposing it flushes the buffer to disk and releases the lock.
Not by itself. A second writer on the same file gets an IOException while the first is open. Use a lock around your Log method, or a logging library.