C# Reference ยท Files and I/O
File.ReadAllText(), ReadAllLines() and ReadLines()
Reference for reading text files in C#: File.ReadAllText, ReadAllLines, ReadLines, ReadAllBytes and async versions, encoding detection, memory use and errors.
The File class has a family of methods that read a whole file in one call. File.ReadAllText returns the content as one string. File.ReadAllLines returns a string[] with one element per line. File.ReadLines returns a lazy IEnumerable<string> that reads line by line while you loop. File.ReadAllBytes returns the raw bytes. All of them open the file, read, and close it again, even when an exception occurs.
Choose by size and by what you do with the data. Small configuration or text files fit ReadAllText; large logs and CSV files should be streamed with ReadLines so that memory use stays constant.
string text = File.ReadAllText("notes.txt"); foreach (string line in File.ReadLines("big.log")) { /* process one 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 string ReadAllText(string path); public static string ReadAllText(string path, Encoding encoding); public static string[] ReadAllLines(string path); public static string[] ReadAllLines(string path, Encoding encoding); public static IEnumerable<string> ReadLines(string path); public static IEnumerable<string> ReadLines(string path, Encoding encoding); public static byte[] ReadAllBytes(string path); public static Task<string> ReadAllTextAsync(string path, CancellationToken cancellationToken = default); public static Task<string[]> ReadAllLinesAsync(string path, CancellationToken cancellationToken = default); public static IAsyncEnumerable<string> ReadLinesAsync(string path, CancellationToken cancellationToken = default); public static Task<byte[]> ReadAllBytesAsync(string path, CancellationToken cancellationToken = default);
| Parameter | Description |
|---|---|
path |
Relative or absolute path of an existing file. |
encoding |
How to turn bytes into characters. The default is UTF-8; a byte order mark (UTF-8, UTF-16, UTF-32) overrides the default. |
cancellationToken |
Stops an async read early. |
Returns: a string, an array, a lazy sequence or a byte array. A line is ended by \n, \r\n or \r, and the terminator is not part of the line. A trailing terminator does not create an extra empty line.
Errors: FileNotFoundException, DirectoryNotFoundException, UnauthorizedAccessException, IOException (file in use, too long), ArgumentException (empty path), OutOfMemoryException for files that do not fit in memory. ReadLines reports a missing file when you call it, but read errors during the loop appear on MoveNext.
Usage notes
- Memory.
ReadAllText,ReadAllLinesandReadAllByteshold the whole file (a string uses two bytes per character). UseReadLines, or aStreamReader, for logs, exports and anything above tens of megabytes. ReadLineskeeps the file open until the loop ends or the enumerator is disposed. Do not write to the same file inside the loop on Windows, and do not callToList()if the goal is to avoid loading everything. Each enumeration reopens the file, so enumerating twice reads it twice.ReadAllLinesvsReadLines. The array has aLengthand index access; the sequence starts producing lines sooner and supportsTake/First. Updating the file while enumeratingReadLinesgives unpredictable results.- Encoding. The default is UTF-8, even on Windows. Legacy ANSI files need an explicit encoding (
Encoding.Latin1, orCodePagesEncodingProviderfor others). Invalid bytes do not throw; they become the replacement character, so a wrongly decoded file shows๏ฟฝinstead of failing. - Line endings are normalized.
ReadAllLinessplits on all three styles, butReadAllTextkeeps the original\r\ncharacters, which then count inLength. - Async methods are worth it in servers and UI programs; for a console tool the sync versions are simpler and slightly faster for small files.
- Don't test
Existsfirst. Call the read method and handleFileNotFoundException; seeFile. File.ReadAllBytesfor binary data. Do not decode binary files as text; useBinaryReaderor aFileStreamfor structured reads.ReadLinesAsyncneeds .NET 7 or later.