How To Read A Text File In C#

The File class provides two static methods to read a text file in C#. The File.ReadAllText() method opens a text file, reads all the text in the file into a string, and then closes the file.
 
The following code snippet reads a text file in to a string.
  1. // Read entire text file content in one string  
  2. string text = File.ReadAllText(textFile);  
  3. Console.WriteLine(text);  
The File.ReadAllLines() method opens a text file, reads all lines of the file into a string array, and then closes the file. The following code snippet reads a text file into an array of strings.
  1. // Read a text file line by line.  
  2. string[] lines = File.ReadAllLines(textFile);  
  3.   
  4. foreach (string line in lines)  
  5. Console.WriteLine(line);  
One more way to read a text file is using a StreamReader class that implements a TextReader and reads characters from a byte stream in a particular encoding. The ReadLine method of StreamReader reads one line at a time.
  1. // Read file using StreamReader. Reads file line by line    
  2. using(StreamReader file = new StreamReader(textFile)) {  
  3.  int counter = 0;  
  4.  string ln;  
  5.   
  6.  while ((ln = file.ReadLine()) != null) {  
  7.   Console.WriteLine(ln);  
  8.   counter++;  
  9.  }  
  10.  file.Close();  
  11.  Console.WriteLine($ "File has {counter} lines.");  

The complete code sample uses the above-discussed methods to read a text file and display its content to the console. To test this code, find a text file (or create one with some text in it) on your machine and change the "textFile" variable to the full path of your .txt file.
  1. using System;  
  2. using System.IO;  
  3.   
  4. namespace ReadATextFile {  
  5.  class Program {  
  6.   // Default folder    
  7.   static readonly string rootFolder = @ "C:\Temp\Data\";    
  8.   //Default file. MAKE SURE TO CHANGE THIS LOCATION AND FILE PATH TO YOUR FILE   
  9.   static readonly string textFile = @ "C:\Temp\Data\Authors.txt";  
  10.   
  11.   static void Main(string[] args) {  
  12.    if (File.Exists(textFile)) {  
  13.     // Read entire text file content in one string    
  14.     string text = File.ReadAllText(textFile);  
  15.     Console.WriteLine(text);  
  16.    }  
  17.   
  18.    if (File.Exists(textFile)) {  
  19.     // Read a text file line by line.  
  20.     string[] lines = File.ReadAllLines(textFile);  
  21.     foreach(string line in lines)  
  22.     Console.WriteLine(line);  
  23.    }  
  24.   
  25.    if (File.Exists(textFile)) {  
  26.     // Read file using StreamReader. Reads file line by line  
  27.     using(StreamReader file = new StreamReader(textFile)) {  
  28.      int counter = 0;  
  29.      string ln;  
  30.   
  31.      while ((ln = file.ReadLine()) != null) {  
  32.       Console.WriteLine(ln);  
  33.       counter++;  
  34.      }  
  35.      file.Close();  
  36.      Console.WriteLine($ "File has {counter} lines.");  
  37.     }  
  38.    }  
  39.   
  40.    Console.ReadKey();  
  41.   }  
  42.  }