C# Read Text File with StreamReader

How to Read Text from A File with StreamReader

This tutorial describes how to read text from a file with StreamReader in C#.

StreamReader provides various methods to read text from a file.

Related tutorial:

ReadToEnd Method

You can write a program to read text from a file using the ReadToEnd method, as the following example shows:

using System;
using System.IO;
 
namespace ReadFileExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string directory = @"D:\Programming Samples";
            string fileName = "sample.txt";
            string filePath = Path.Combine(directory, fileName);
            using (StreamReader reader = new StreamReader(filePath))
            {
                string text = reader.ReadToEnd();
                Console.WriteLine(text);
            }
        }
    }
}

The above program read text from a file named sample.txt and writes output to the console.

For example, let's say sample.txt contains five lines, Line 1, Line2, Line 3, Line 4, and Line 5.

The program produces the output, as shown below.

Line 1
Line 2
Line 3
Line 4
Line 5

ReadLine Method

ReadLine method reads a line of characters from the current stream.

The following example shows how to read text from a file line by line by using the ReadLine and Peek methods:

using System;
using System.IO;
 
namespace ReadFileExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string directory = @"D:\Programming Samples";
            string fileName = "sample.txt";
            string filePath = Path.Combine(directory, fileName);
            using (StreamReader reader = new StreamReader(filePath))
            {
                while (reader.Peek() > 0)
                {
                    string text = reader.ReadLine();
                    Console.WriteLine(text);
                }
            }
        }
    }
}

The above program reads text from a file named smaple.txt line by line and writes output to the console.

You can also use the File class to read from a text file.

The following example read all text from a file as one string:

using System;
using System.IO;
 
namespace ReadFileExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string directory = @"D:\Programming Samples";
            string fileName = "sample.txt";
            string filePath = Path.Combine(directory, fileName);
            string text = File.ReadAllText(filePath);
            Console.WriteLine(text);
        }
    }
}

Or you read each line of the file into a string array, as the following example shows:

using System;
using System.IO;
 
namespace ReadFileExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string directory = @"D:\Programming Samples";
            string fileName = "sample.txt";
            string filePath = Path.Combine(directory, fileName);
            string[] lines = File.ReadAllLines(filePath);
            foreach (var line in lines)
            {
                Console.WriteLine(line);
            }
        }
    }
}

In this tutorial, you have learned how to read text from a file with StreamReader and the File class in C#.

With StreamReader, you can write a program to read line by line or all lines at once from a file.


See also:
What is StringBuilder in C# and How to Use it
C# Write to File with the File Class and StreamWriter
C# switch case Statements with Examples
C# if-else Statements with Examples
C# while Loop Statement with Examples