0
0
CsharpHow-ToBeginner · 4 min read

How to Read a File in C#: Simple Guide with Examples

In C#, you can read a file using File.ReadAllText to get the entire content as a string or File.ReadAllLines to get an array of lines. These methods are part of the System.IO namespace and are easy to use for reading text files.
📐

Syntax

The basic syntax to read a file in C# uses methods from the System.IO.File class:

  • File.ReadAllText(string path): Reads the whole file content as a single string.
  • File.ReadAllLines(string path): Reads the file and returns an array of strings, each representing a line.

You need to provide the file path as a string to these methods.

csharp
string content = File.ReadAllText("path/to/file.txt");
string[] lines = File.ReadAllLines("path/to/file.txt");
💻

Example

This example shows how to read a text file completely and print its content to the console.

csharp
using System;
using System.IO;

class Program
{
    static void Main()
    {
        string path = "example.txt";

        // Read entire file content
        string content = File.ReadAllText(path);
        Console.WriteLine("File content using ReadAllText:");
        Console.WriteLine(content);

        // Read file line by line
        string[] lines = File.ReadAllLines(path);
        Console.WriteLine("\nFile content using ReadAllLines:");
        foreach (string line in lines)
        {
            Console.WriteLine(line);
        }
    }
}
Output
File content using ReadAllText: Hello, this is a sample file. It has multiple lines. This is the third line. File content using ReadAllLines: Hello, this is a sample file. It has multiple lines. This is the third line.
⚠️

Common Pitfalls

Common mistakes when reading files in C# include:

  • Not handling exceptions like FileNotFoundException or UnauthorizedAccessException.
  • Using incorrect file paths or forgetting to include the file extension.
  • Trying to read very large files with ReadAllText which can cause memory issues.

Always use try-catch blocks to handle errors gracefully.

csharp
try
{
    string content = File.ReadAllText("missingfile.txt");
}
catch (FileNotFoundException)
{
    Console.WriteLine("File not found. Please check the path.");
}

// Correct way with error handling
try
{
    string content = File.ReadAllText("example.txt");
    Console.WriteLine(content);
}
catch (Exception ex)
{
    Console.WriteLine($"Error reading file: {ex.Message}");
}
📊

Quick Reference

MethodDescription
File.ReadAllText(path)Reads entire file content as a single string.
File.ReadAllLines(path)Reads file and returns an array of lines.
File.ReadLines(path)Reads file lazily line by line (good for large files).
StreamReaderUse for more control over reading files, like reading line by line with buffering.

Key Takeaways

Use File.ReadAllText to read the whole file content as one string.
Use File.ReadAllLines to get an array of lines from the file.
Always handle exceptions to avoid crashes when the file is missing or inaccessible.
For large files, consider reading line by line with File.ReadLines or StreamReader.
Provide the correct file path including the file name and extension.