C#:
Reading a text file

How to:

Let’s get straight to it. Here’s how you read from a file in C# using System.IO.

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = @"C:\path\to\your\file.txt";
        
        // Reading all text
        string allText = File.ReadAllText(filePath);
        Console.WriteLine(allText);
        
        // Reading lines into an array
        string[] lines = File.ReadAllLines(filePath);
        foreach (var line in lines)
        {
            Console.WriteLine(line);
        }
        
        // Reading with a StreamReader
        using (StreamReader reader = new StreamReader(filePath))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                Console.WriteLine(line);
            }
        }
    }
}

Sample Output:

Hello, this is a text file.
It has multiple lines.
Each line will be read separately.

Deep Dive

Reading a text file seems simple enough, right? But there’s a bit of history and some nuances worth knowing.

Back in the day, text files were often the primary way to store data before databases were commonly used. Programmers had to manage file access, format data correctly, and handle errors. C# has evolved a lot since then. Now, System.IO is your go-to namespace for file operations.

You’ve got options:

  • File.ReadAllText reads the whole shebang in one go—great for smaller files.
  • File.ReadAllLines gives you each line as an array element—handy for processing lines.
  • StreamReader reads line-by-line, which is more memory efficient for big files.

Each method locks the file while it’s in use. This is important if other processes might be trying to access the file.

Remember, always handle exceptions such as FileNotFoundException or IOException when dealing with files. You don’t want your app crashing unexpectedly.

See Also

Have more questions or looking to expand your knowledge? Check out these links: