Read Text File In .NET MVC

In this article, we will learn, how we can read a Text file using C#, To read from file we have two class File and FileInfo which help us to read a text from a file.

The ‘File’ class holds static methods and offers security checks on all the methods. The FileInfo holds instance methods and security check is not needed. For reading from a text file, we have abstract classes like the TextReader class.

public void Read()
{
  //path of file
  var path = @"E:\TheCodeHubs.txt";

  if (System.IO.File.Exists(path))
  {
   using (TextReader tr = new StreamReader(path))
   {
     var text = tr.ReadLine();
   }
  }
}

If you are looking for a write-in a file using c#, you can read from here.

1 Comment

  1. baccarat

    Love sand and sand clock !

    0
    0
    Reply

Submit a Comment

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

Subscribe

Select Categories