Writing to a file using FileStream in VB.NET

In this article we will learn how to create file and write to it using FileStream in VB.NET

Here I am using FileStream object to write contents to a file.
First a FileSteam and File object is created. Using file object we will compare wether file is exists are not.
If file exits in the disk then we will truncate it otherwise we will create new file and allow that file for read and write using file steam object.

Public

Sub WriteErrorReport(ByVal message As String, ByVal FileName As String)

Dim
f As File
Dim fstream As FileStream
If f.Exists(FileName) Then
            fstream = New FileStream(FileName, FileMode.Truncate, FileAccess.ReadWrite)
Else
            fstream = New FileStream(FileName, FileMode.Create, FileAccess.ReadWrite)
End If
Dim sWriter As New StreamWriter(fstream)
sWriter.BaseStream.Seek(0, SeekOrigin.Begin)
sWriter.Write(message)
sWriter.Close()

End
Sub

By Cos mos   Popularity  (5680 Views)