Friday 8 June 2012

.Net Beginners , C# Tips : How to Open a file and return a FileStream

You can get the FileStream of a file by opening a file using File Class.
After get FileStream and completing work on file stream you have to close FileSteram using Close method. to avoid any file open errors.

Here are example for this.
C# Example :
System.IO.FileStream stream1 = System.IO.File.Open(MapPath("TextFile.txt"), System.IO.FileMode.Open);
stream1.Close();

VB.net Example :
Dim stream1 As System.IO.FileStream = System.IO.File.Open(MapPath("TextFile.txt"), System.IO.FileMode.Open)
stream1.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