C# Program to Read a File Using Stream Reader

Here is an example as how to write a file using stream writer command —–
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication20
{
    class FileRead
    {
        public void ReadData()
        {
            {
                FileStream fs = new FileStream(“C://sohan.txt”, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                sr.BaseStream.Seek(0, SeekOrigin.Begin);
                string str = sr.ReadLine();
                while (str != null)
                {
                    Console.WriteLine(“{0}”, str);
                    str = sr.ReadLine();
                }
                sr.Close();
                fs.Close();
            }
        }
        public static void Main(String[] args)
        {
            FileRead fr = new FileRead();
            fr.ReadData();
            Console.ReadLine();
        }
    }
}

Add Comment