How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a string variable and then closes the file.

1.Read whole data of Text file:

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

2.Read data line by line:

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.

string [] lines = File.ReadAllLines(textFileCompletePath);

foreach (string line in lines)
Console.WriteLine(line);

3. Read a text file is using a StreamReader:

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.

using System;
using System.IO;

namespace ReadTextFromFile {
 class Program {
  static readonly string rootFolder = @ "C:\Temp\Data\";
  static readonly string textFile = @ "C:\Data\Student.txt";

  static void Main(string[] args) {
   if (File.Exists(textFileCompletePath)) 
   {
    // Read entire text file content in one GO
    string text = File.ReadAllText(textFileCompletePath);
    Console.WriteLine(text);
   }

   if (File.Exists(textFileCompletePath)) 
   {
    // Read a text file data line by line.
    string[] lines = File.ReadAllLines(textFileCompletePath);
    foreach(string line in lines)
    Console.WriteLine(line);
   }

   if (File.Exists(textFileCompletePath)) 
   {
    // Read file using StreamReader. Reads file line by line
    using(StreamReader file = new StreamReader(textFileCompletePath)) 
    {
     int counter = 0;
     string ln;
     while ((ln = file.ReadLine()) != null) {
      Console.WriteLine(ln);
      counter++;
     }
     file.Close();
     Console.WriteLine($ "File has {counter} lines.");
    }
   }

   Console.ReadKey();
  }
 }
}

Till now we learned about reading of data from text file, below we, learn more about other methods of File class.

Working With File Class Methods in C#:

C# File Class methods:

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check out the chart below.

List of the common File class methods:

Sr. No.Method NameDescription
1.CopyThis method is used to copy given file to the specified location.
2.CreateThis method is used to create a file in the specified path.
3.DeleteThis method is used to Delete a file.
4.OpenThis method is used to return a filestream object at the specified path.
5.MoveMoves a given file to a new location. We can also specify a different or new name for the file in the new location.
6.ExistsThis method is used to check whether the specified file exists or not. It returns boolean=true if file exists else will return false.
7.OpenWriteIt is used to opens an existing file or creates a new file for writing.
8.OpenReadIt is used to opens an existing file for reading.

We hope with above post you learned how to use the File class in C# and how to read a text file data using various methods of C#. Check more on above methods of File class in C#.

Leave a Reply

Your email address will not be published. Required fields are marked *