Saturday 2 December 2017

C# Program to Read the Contents of the File


Code:

using System;
using System.IO;
class FileRead
{
    public void readdata()
    {
        FileStream fs = new FileStream("Myfile.txt", FileMode.Open, FileAccess.Read);
        StreamReader sr = new StreamReader(fs);//Position the File Pointer at the Beginning of the File
        sr.BaseStream.Seek(0, SeekOrigin.Begin);//Read till the End of the File is Encountered
        string str = sr.ReadLine();
        while (str != null)
        {
            Console.WriteLine("{0}", str);
            str = sr.ReadLine();
        }
        //Close the Writer and File
        sr.Close();
        fs.Close();
    }
    public static void Main(String[] args)
    {
        FileRead fr = new FileRead();
        fr.readdata();
    }
}


Output:

The text which your are reading are read from the file named myfile.txt that is created already.



More C# Programs:


















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...