find and solve || findandsolve.com
Please wait.....

C# StreamReader

In the C#, StreamReader is used to read characters to a stream in a specified encoding. StreamReader.Read method reads the next character or next which is inherits TextReader class.

C# StreamReader example to read one line

using System; 
using System.IO;
namespace FindAndSolve {
    public class Program
    {
        public static void Main(string[] args)
        {
      FileStream file = new FileStream("e:\\StreamReaderExample.txt", FileMode.OpenOrCreate);
         StreamReader objStream = new StreamReader(file);
         string line=objStream.ReadLine();          Console.WriteLine(line);
         objStream.Close();
         file.Close();
        }
    }
}

Output

Hello FindAndSolve

C# StreamReader example to read all lines

using System; 
using System.IO;
namespace FindAndSolve {
    public class Program     {         public static void Main(string[] args)
        {
      FileStream file = new FileStream("e:\\multiLineExample.txt", FileMode.OpenOrCreate);
        StreamReader objStream = new StreamReader(file);
        string line = "";
        while ((line = objStream.ReadLine()) != null)
        {
            Console.WriteLine(line);         }
        objStream.Close();
        file.Close();
        }
    }
}

Output

Hello FindAndSolve

this is file handling