read a specific line from a text file c# code example

Example 1: c# read from file line by line

string line;

// Read the file and display it line by line.  
System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt");  
while((line = file.ReadLine()) != null)  
{  
    System.Console.WriteLine(line);
}  
  
file.Close();

Example 2: how to read particular line of file in c#

string GetLine(string fileName, int line)
{
   using (var sr = new StreamReader(fileName)) {
       for (int i = 1; i < line; i++)
          sr.ReadLine();
       return sr.ReadLine();
   }
}