FileStream in VB.NET
In this article I will explain you about FileStream in VB.NET
The System.IO namespace is contain file handling in visual basic with a class library that supports string, character and file manipulation, these classes perform creating, copying, moving, and deleting files operation's with the help of properties, methods and events. Since both strings and numeric data types are supported, they also allow us to incorporate data types in files. The most commonly used classes are FileStream, BinaryReader, BinaryWriter, StreamReader and StreamWriter.
Here we discuss about the FileStream Class.
Nine overloaded constructors help you gain finer control over the file states. The constructor most commonly used, shown in below example, helps set the various access permissions and creation states on the file through the use of the FileMode, FileAccess, and FileShare enumerations.
FileStream Constructor
Dim fs As New FileStream("file.doc", FileMode.Create, FileAccess.Write)
FileMode Enumeration
The FileMode enumeration helps you the set the mode in which you want to open the file. You can use these modes to set your file up for appending or overwriting or initial creation, as detailed in Table1.1.
Table 1.1: Enumeration FileMode
Note that the Append mode can only be used when FileAccess.Write permission (described next) is set.
FileAccess Enumeration
With the FileAccess enumerations described in Table 1.2, you can set the mode of access to a file. It's never good to authorize more access than needed-or less access than needed, for that matter. Choose Read when you intend to read from a file and Write when you write to a file. Remember, though, that if you specify Read access and later try to write to the file, an exception will be raised. The same applies when you specify Write access and try to read the file later.
Table 1.2: Enumeration FileAccess
FileShare Enumeration
The FileShare enumeration, detailed in Table 1.3, is very important if you wish to share your file with other processes. For example, suppose you have an XML file acting as a database file for an ASP.NET application. If you don't specify the FileAccess enumeration, only one user can read from the XML database file at a time; other concurrent users encounter an error when accessing the database because the FileShare.None enumeration is implemented by default.
Table 1.3: Enumeration FileShare
The short example given below demonstrates how you can work with the FileStream class.
Drag a Button and a RichTextBox control onto the form. Paste the following code which is shown below.
Example Code
Imports System.IO
Imports System.Windows.Forms.Form
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FileS As New FileStream("file.doc", FileMode.Create, FileAccess.Write)
'declaring a FileStream and creating a document file named file with
'access mode of writing
Dim a As New StreamWriter(FileS)
'creating a new StreamWriter and passing the filestream object fs as argument
a.WriteLine("This is an example of FileStream in VB .NET.")
'writing text to the newly created file
a.Close()
'closing the file
FileS = New FileStream("file.doc", FileMode.Open, FileAccess.Read)
'declaring a FileStream to open the file named file.doc with access mode of reading
Dim i As New StreamReader(FileS)
'creating a new StreamReader and passing the filestream object fs as argument
i.BaseStream.Seek(0, SeekOrigin.Begin)
'Seek method is used to move the cursor to different positions in a file, in this code, to
'the beginning
While i.Peek() > -1
'peek method of StreamReader object tells how much more data is left in the file
RichTextBox1.Text &= i.ReadLine()
'displaying text from doc file in the RichTextBox
End While
i.Close()
End Sub
End Class
Output
Conclusion
Hope this article would have helped you in understanding FileStream in VB.NET