C# Reference · Files and I/O
File.WriteAllText(), WriteAllLines() and AppendAllText()
Reference for writing text files in C#: File.WriteAllText, WriteAllLines, AppendAllText, WriteAllBytes and async versions, encodings, BOM, overwriting and safe saving.
The write methods of File create a file, write everything you give them, and close it, all in one call. File.WriteAllText writes a string, File.WriteAllLines writes a sequence of lines, and File.WriteAllBytes writes raw bytes. They replace an existing file. To add to the end of a file instead, use File.AppendAllText or File.AppendAllLines, which create the file if it is missing.
These methods are ideal for small files such as settings, reports and test fixtures. For data that arrives piece by piece, or for very large output, keep a StreamWriter open instead.
File.WriteAllText("hello.txt", "Hello, file!"); File.AppendAllText("hello.txt", "\nSecond line");
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static void WriteAllText(string path, string? contents); public static void WriteAllText(string path, string? contents, Encoding encoding); public static void WriteAllLines(string path, IEnumerable<string> contents); public static void WriteAllLines(string path, string[] contents, Encoding encoding); public static void WriteAllBytes(string path, byte[] bytes); public static void AppendAllText(string path, string? contents); public static void AppendAllLines(string path, IEnumerable<string> contents); public static Task WriteAllTextAsync(string path, string? contents, CancellationToken cancellationToken = default); public static Task AppendAllTextAsync(string path, string? contents, CancellationToken cancellationToken = default);
| Parameter | Description |
|---|---|
path |
File to create, overwrite or append to. The folder must exist. |
contents |
Text, lines or bytes to write. A null string is written as an empty file. |
encoding |
Character encoding; the default is UTF-8 without a byte order mark. |
cancellationToken |
Cancels an async write. |
Returns: nothing (void or Task).
Errors: DirectoryNotFoundException (missing folder), UnauthorizedAccessException (no permission, read-only file or the path is a folder), IOException (file locked, disk full), ArgumentException (empty path), ArgumentNullException (null path or null lines).
Usage notes
- Overwrite is silent.
WriteAllText,WriteAllLinesandWriteAllBytestruncate an existing file without asking. Check withFile.Existsfirst, or open withFileMode.CreateNewthrough aFileStreamwhen overwriting would be a bug. - Folders are not created. Call
Directory.CreateDirectory(Path.GetDirectoryName(path)!)first; it is safe to call when the folder exists. - Line terminator.
WriteAllLinesends every line withEnvironment.NewLine(\non Linux and macOS,\r\non Windows), including the last one.AppendAllTextadds nothing, so put the\nin your string. - UTF-8 and the BOM. With no encoding argument the file is UTF-8 without BOM. Passing
Encoding.UTF8writes a BOM, which some tools (shell scripts, JSON parsers, Unix tools) dislike. Usenew UTF8Encoding(false)for BOM-free output. When you read,File.ReadAllTextremoves a BOM automatically. - Not atomic. If the program crashes in the middle of a write, the file can be left half-written. For files that must stay valid, write to a temporary file in the same folder and then call
File.ReplaceorFile.Move(temp, target, overwrite: true). - Concurrent writers. Two threads or processes appending to one file can interleave or fail with
IOException. Serialize access with a lock, or give each writer its own file. - Memory.
WriteAllLineswith a lazy sequence streams the lines, whileWriteAllTextneeds the complete string in memory. For large output use aStreamWriter. AppendAllLinesAsyncandWriteAllLinesAsyncexist in current .NET versions for async code.- Durability. The data may still sit in the operating system cache after the call returns. Use
FileStream.Flush(true)when it must reach the disk.