In few previous articles, I have explained how we can read JSON data in C# and how to read excel file in C#, now in this article, I have provided code sample using console application to show how to read a text file in C# line by line or reading entire text file as a string in one by go using C#.

Let's a look at each of the example code, one in which text file is read and converted into string, i.e, using System.IO.ReadAllText() and another is reading text file line by line using System.IO.ReadAllLines() which returns array of line, and we can loop that array to print each line of text file.

Reading file in C# line by line

In this example, we will read a text file line by line using System.IO.ReadALLLines() in console application.

So, if you are new to C# or Visual Studio, you can create a new Console application by opening Visual Studio, navigating to "New"-> "Project" -> Select "Windows Classic" from left-pane and "Console app (Windows Application)"-> Give a name to your project "ReadInCSharp" and click "OK"

read-text-file-in-csharp-min.png

Now, inside Program.cs, we will write our code

using System;
using System.IO;

namespace ReadInCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            //file in disk
            var FileUrl = @"D:\testFile.txt";

            //file lines
            string[] lines = File.ReadAllLines(FileUrl);

            //loop through each file line
            foreach (string line in lines)
            {
                Console.WriteLine(line);
            }

        }
    }
}

Output:

This is test file.
To Read text file in C#
Sample.

C-sharp-read-file-line-by-line-min.png

In the above, code we are using foreach loop to read all lines of an string array.

Reading text in C# all line at once

Let's take a look at C# code to read all lines at once of a text file.

using System;
using System.IO;

namespace ReadInCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            //file in disk
            var FileUrl = @"D:\testFile.txt";

            // Read entire text file content in one string  
            string text = File.ReadAllText(FileUrl);
            Console.WriteLine(text);


        }
    }
}

Output:

This is test file.
To Read text file in C#
Sample.

read-all-lines-c-sharp-min.png

Reading Text file using StreamReader

There is one more way to read lines of a text file in C#, which is using StreamReader.

StreamReader class implements a TextReader that reads characters from a byte stream in a particular encoding.

using System;
using System.IO;

namespace ReadInCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            //file in disk
            var FileUrl = @"D:\testFile.txt";

            try
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader(FileUrl))
                {
                    string line;
                   //read the line by line and print each line
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                    }
                }
            }
            catch (Exception e)
            {
                // Something went wrong.
                Console.WriteLine("The file could not be read:");
                //print error message
                Console.WriteLine(e.Message);
            }


        }
    }
}

Output is same as above, in the abovde code, we are using StreamReader instance to read text from file.

streamreader-read-text-file-csharp-min.png

As you can see in the above code, we are feeding the File url to "StreamReader" class object and then we are reading file line by line using sr.ReadLine(), which gives us one line at a time from text file, then using Console.WriteLine(), we are printing the value of that line console application.