File stream operations in VB.net

By: Issac Viewed: 153249 times  Printer Friendly Format    


The FileStream class allows us to move data to and from the stream as arrays of bytes, a filestream actually symbolizes a a file in a system. We operate File using FileMode in FileStream Class

Some of the FileModes are as Follows : 

FileMode.Append: It opens and appends to a file, if the file does not exist, it creates a new file
FileMode.Create: Creates a new file, if the file already exists it will appends to it.
FileMode.CreateNew: Creates a new file, if the file exists it throws an exception.
FileMode.Open: Opens an existing file

How to create a file using VB.NET FileStream? 

The following example shows, how to write in a file using FileStream.

Imports System.IO
Imports System.Text
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim wFile As System.IO.FileStream
Dim byteData() As Byte
byteData = Encoding.ASCII.GetBytes("FileStream Test1")
wFile = New FileStream("streamtest.txt", FileMode.Append)
wFile.Write(byteData, 0, byteData.Length)
wFile.Close()
Catch ex As IOException
MsgBox(ex.ToString)
End Try
End Sub
End Class

When we execute the program, it creates a new File and writes the content to it.

Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)

Comment on this tutorial