Back to blog
May 11, 2025
4 min read

How to Read a File in C#: Quick and Efficient Methods

Reading files is a fundamental skill in C#, and knowing the right method can improve both performance and readability. This article explores various ways to read text files, from the simplest to the most efficient, with practical code examples and guidance.

Read


Table of Contents

  1. ReadAllText – The Quickest Way
  2. ReadAllLines – Easy Line-by-Line
  3. ReadLines – Best for Large Files
  4. StreamReader – Full Control
  5. Asynchronous Reading
  6. Benchmark Example
  7. Encoding Considerations
  8. File Existence Check
  9. Comparison Summary
  10. GitHub Example
  11. Frequently Asked Questions (FAQ)

1. ReadAllText – The Quickest Way

string content = File.ReadAllText("example.txt");
Console.WriteLine(content);

Best for small files when you need the entire content at once.


2. ReadAllLines – Easy Line-by-Line

string[] lines = File.ReadAllLines("example.txt");
foreach (var line in lines)
{
    Console.WriteLine(line);
}

Ideal when processing individual lines but the file size is moderate.


3. ReadLines – Best for Large Files

foreach (var line in File.ReadLines("example.txt"))
{
    Console.WriteLine(line);
}

This method uses lazy evaluation, making it memory-efficient for large files.


4. StreamReader – Full Control

using (var reader = new StreamReader("example.txt", Encoding.UTF8))
{
    string? line;
    while ((line = reader.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}

Use when you need to specify encoding or implement custom logic for each line.


5. Asynchronous Reading

Reading an entire file asynchronously:

string content = await File.ReadAllTextAsync("example.txt");
Console.WriteLine(content);

For asynchronous line-by-line reading:

await foreach (var line in ReadLinesAsync("example.txt"))
{
    Console.WriteLine(line);
}

async IAsyncEnumerable<string> ReadLinesAsync(string path)
{
    using var reader = new StreamReader(path);
    while (await reader.ReadLineAsync() is { } line)
    {
        yield return line;
    }
}

Recommended for GUI applications or web servers where blocking is undesirable.


6. Benchmark Example

var sw = Stopwatch.StartNew();
string content = File.ReadAllText("example.txt");
sw.Stop();
Console.WriteLine($"Elapsed time: {sw.ElapsedMilliseconds} ms");

Use similar code to compare different reading methods in your environment.


7. Encoding Considerations

To specify a particular encoding:

string content = File.ReadAllText("example.txt", Encoding.GetEncoding("Windows-1252"));

Useful encodings include:

  • Encoding.UTF8 (default)
  • Encoding.Unicode (UTF-16)
  • Encoding.ASCII
  • Encoding.GetEncoding("ISO-8859-1")

8. File Existence Check

Always check if the file exists to avoid exceptions:

if (!File.Exists("example.txt"))
{
    Console.WriteLine("File not found.");
    return;
}

9. Comparison Summary

MethodBest ForMemory UseAsyncLazy
ReadAllTextSmall filesHighYesNo
ReadAllLinesSmall/medium filesHighYesNo
ReadLinesLarge filesLowNoYes
StreamReaderFull controlMediumYesYes
ReadAllTextAsyncAsync + small filesHighYesNo
StreamReader + asyncAsync + large filesLowYesYes

10. GitHub Example

The full example project will be available at:

https://github.com/AdrianBailador/FileReadingDemo


11. Frequently Asked Questions (FAQ)

What is considered a large file to read into memory?

As a general rule:

  • Under 10 MB: Safe for full read
  • 10–100 MB: Use with caution
  • Over 100 MB: Use streaming (ReadLines or StreamReader)

How to read a CSV file in C#?

For simple CSV parsing:

foreach (var line in File.ReadLines("data.csv"))
{
    var fields = line.Split(',');
    Console.WriteLine($"Name: {fields[0]}, Age: {fields[1]}");
}

For complex CSV files, use a library like CsvHelper.

How to check if a file exists before reading it in C#?

Use File.Exists:

if (!File.Exists("example.txt"))
{
    Console.WriteLine("The file does not exist.");
    return;
}