Skip to content
Tutorials Center
My learning

Topics

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.

C#
string text = File.ReadAllText("notes.txt");
foreach (string line in File.ReadLines("big.log")) { /* process one line */ }

Try it

C# ReadAllText, ReadAllLines and ReadLines

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, errors and async reading

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 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, ReadAllLines and ReadAllBytes hold the whole file (a string uses two bytes per character). Use ReadLines, or a StreamReader, for logs, exports and anything above tens of megabytes.
  • ReadLines keeps 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 call ToList() if the goal is to avoid loading everything. Each enumeration reopens the file, so enumerating twice reads it twice.
  • ReadAllLines vs ReadLines. The array has a Length and index access; the sequence starts producing lines sooner and supports Take/First. Updating the file while enumerating ReadLines gives unpredictable results.
  • Encoding. The default is UTF-8, even on Windows. Legacy ANSI files need an explicit encoding (Encoding.Latin1, or CodePagesEncodingProvider for others). Invalid bytes do not throw; they become the replacement character, so a wrongly decoded file shows ๏ฟฝ instead of failing.
  • Line endings are normalized. ReadAllLines splits on all three styles, but ReadAllText keeps the original \r\n characters, which then count in Length.
  • 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 Exists first. Call the read method and handle FileNotFoundException; see File.
  • File.ReadAllBytes for binary data. Do not decode binary files as text; use BinaryReader or a FileStream for structured reads.
  • ReadLinesAsync needs .NET 7 or later.