Skip to content
Tutorials Center
My learning

Topics

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.

C#
File.WriteAllText("hello.txt", "Hello, file!");
File.AppendAllText("hello.txt", "\nSecond line");

Try it

C# Write, append and read back

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.

C# Encodings, BOM and errors

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

C#
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, WriteAllLines and WriteAllBytes truncate an existing file without asking. Check with File.Exists first, or open with FileMode.CreateNew through a FileStream when 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. WriteAllLines ends every line with Environment.NewLine (\n on Linux and macOS, \r\n on Windows), including the last one. AppendAllText adds nothing, so put the \n in your string.
  • UTF-8 and the BOM. With no encoding argument the file is UTF-8 without BOM. Passing Encoding.UTF8 writes a BOM, which some tools (shell scripts, JSON parsers, Unix tools) dislike. Use new UTF8Encoding(false) for BOM-free output. When you read, File.ReadAllText removes 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.Replace or File.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. WriteAllLines with a lazy sequence streams the lines, while WriteAllText needs the complete string in memory. For large output use a StreamWriter.
  • AppendAllLinesAsync and WriteAllLinesAsync exist 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.