Monday, 11 June 2012

.Net Beginners , C# Tips : How to Open a file and return a StreamReader for reading the data

You are able to get stream reader of a file and work on stream reader.
After getting Stream Reader and completing work on stream reader you have to close SteramReader using Close method. to avoid any file open errors.

Here are example for this.

C# Example :
System.IO.StreamReader objStreamReader = System.IO.File.OpenText(MapPath("TextFile.txt"));
objStreamReader.Close();

VB.net Example :
Dim objStreamReader As System.IO.StreamReader = System.IO.File.OpenText(MapPath("TextFile.txt"))
objStreamReader.Close()

Here in below image you can see that after FileStream Object is filled it's properties are set.

Output :

No comments:

Post a Comment