Tuesday 19 June 2012

.Net Beginners , C# Tips : How to Open a filestream for reading a stream using C# .Net Programming and VB.Net Programming

You can open a file in read mode and get FileStream Object for reading a stream.
After getting FileStream object you can read data using "Read" and "ReadByte" methods.
You can open any file i.e Text , Image , doc etc..
After reading the file you need to close the FileStream Object.

Here are example for this.
In this example we open two different "Text" and "Image" format files.

C# Example :
    // Get a filestream for reading a Text file
    System.IO.FileStream objFileStreamTxt = System.IO.File.OpenRead(MapPath("TextFile.txt"));
    objFileStreamTxt.Close();

    // Get a filestream for reading a Image file
    System.IO.FileStream objFileStreamImg = System.IO.File.OpenRead(MapPath("computer.jpg"));
    objFileStreamImg.Close();

VB.net Example :
        ' Get a filestream for reading a Text file
        Dim objFileStreamTxt As System.IO.FileStream = System.IO.File.OpenRead(MapPath("TextFile.txt"))
        objFileStreamTxt.Close()

        ' Get a filestream for reading a Image file
        Dim objFileStreamImg As System.IO.FileStream = System.IO.File.OpenRead(MapPath("computer.jpg"))
        objFileStreamImg.Close()

Here is example of how to read FileStream object using "Read" method. Click Here...

This is very useful .Net Tips which is use in day to day programming life.

No comments:

Post a Comment