Skip to main content

Read text file using C#’s StreamReader

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: using System.IO;
  6: 
  7: namespace ConsoleApplication1
  8: {
  9:     class Program
 10:     {
 11:         static void Main(string[] args)
 12:         {
 13: 	    // text file to read
 14:             string fileName = "YourFileName.extension";						
 15:             StreamReader sr = new StreamReader(fileName, Encoding.Default); 
 16:             string tempS = "";
 17:             while (sr.EndOfStream != true)
 18:             {
 19: 		// read a line a time
 20:                 tempS = sr.ReadLine();
 21: 		// print each line on console				
 22:                 Console.Out.WriteLine(tempS); 
 23:             }
 24:         }
 25:     }
 26: }

Comments