File.ReadAllText() / File.WriteAllText()
How to use File.ReadAllText() to read the entire contents of a text file at once, and File.WriteAllText() to write text all at once.
Syntax
using System.IO; // Reads all content from a file and returns it as a string. string content = File.ReadAllText(string path); string content = File.ReadAllText(string path, Encoding encoding); // Writes a string to a file (overwrites existing content). File.WriteAllText(string path, string? contents); File.WriteAllText(string path, string? contents, Encoding encoding); // Reads all lines from a file and returns them as a string array (one element per line). string[] lines = File.ReadAllLines(string path); // Writes a string array to a file, one line at a time (overwrites existing content). File.WriteAllLines(string path, IEnumerable<string> contents); // Appends text to the end of a file. File.AppendAllText(string path, string? contents);
Method List
| Method | Description |
|---|---|
| File.ReadAllText(path) | Opens a text file, reads all its content, and returns it as a string. The default encoding is UTF-8. |
| File.WriteAllText(path, contents) | Creates or overwrites a file with the specified content. If the file does not exist, it is created. |
| File.ReadAllLines(path) | Reads a text file and returns each line as an element of a string array. |
| File.WriteAllLines(path, contents) | Writes a sequence of strings to a file, one line at a time (overwrites existing content). |
| File.AppendAllText(path, contents) | Appends a string to the end of a file. If the file does not exist, it is created. |
| File.ReadAllBytes(path) | Reads a file and returns its content as a byte array. Used for binary files. |
Sample Code
using System;
using System.IO;
using System.Text;
string filePath = "sample.txt";
// Create a file using File.WriteAllText().
string writeContent = "Line 1: Hello, C#\nLine 2: File I/O\nLine 3: Done";
File.WriteAllText(filePath, writeContent, Encoding.UTF8);
Console.WriteLine("File written.");
// Read all content from the file using File.ReadAllText().
string readContent = File.ReadAllText(filePath, Encoding.UTF8);
Console.WriteLine("--- File Content ---");
Console.WriteLine(readContent);
// Read the file line by line using File.ReadAllLines().
string[] lines = File.ReadAllLines(filePath, Encoding.UTF8);
Console.WriteLine($"Line count: {lines.Length}");
foreach (string line in lines)
{
Console.WriteLine($" [{line}]");
}
// Append text to the file using File.AppendAllText().
File.AppendAllText(filePath, "\nLine 4: Appended", Encoding.UTF8);
// Verify the content after appending.
string afterAppend = File.ReadAllText(filePath);
Console.WriteLine($"Line count (after append): {File.ReadAllLines(filePath).Length}");
// Clean up by deleting the file.
File.Delete(filePath);
Console.WriteLine("File deleted.");
Notes
File.ReadAllText() and File.WriteAllText() load the entire file into memory at once. For large files (tens of MB or more), this may cause an out-of-memory error. When working with large files, consider using StreamReader / StreamWriter, which process the file one line at a time.
If you omit the encoding parameter, the default in .NET 5 and later is UTF-8 (without BOM). To work with other encodings such as Shift-JIS, specify Encoding.GetEncoding("shift_jis"). For checking file existence, deleting, and copying files, see File.Exists() / Delete() / Copy().
If you find any errors or copyright issues, please contact us.