BinaryReader and BinaryWriter Classes in VB.NET

In this article I will explain you about BinaryReader and BinaryWriter Classes in VB.NET
  • 3043
 

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 BinaryReader, and BinaryWriter classes.

These classes are used to read and write primitive data types and strings. If you deal only with primitive types, this is the best stream to use. Remember that this data is not easily readable by a human eyeing its contents since the data is read in its binary form.

The following code example demonstrates how to store and retrieve application settings in a file.

Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Security.Permissions

Public Class Test
    Shared Sub Main()
        Dim SettingA As New SettingA()
        Console.WriteLine("Load, store & retrieve settings")
        Console.WriteLine()
        Console.WriteLine("==App settings==" & vbCrLf & "Aspect " & "Ratio: {0}, Lookup directory: {1}," & vbCrLf & "Auto " & "save time: {2} minutes, Show status
        bar: {3}"
& vbCrLf, New Object(3) {SettingA.AspectRatio.ToString(), SettingA.LookupDir, SettingA.AutoTime.ToString(), SettingA.ShowStatusBar.ToString()})
        Console.WriteLine("Change settings.")
        SettingA.AspectRatio = 1.22
        SettingA.LookupDir =
"C:\Temp"
        SettingA.AutoTime = 11
        SettingA.ShowStatusBar =
True
        SettingA.Close()
        Console.ReadLine()
    End Sub
End
Class

Public
Class SettingA
    Const fileName As String = "VBSettingA#@@#.dat"
    Dim aspRatio As Single
    Dim
Dir As String
    Dim
Time As Integer
    Dim
statusBar As Boolean
    Property
AspectRatio() As Single
        Get
            Return
aspRatio
        End Get
        Set
(ByVal value As Single)
            aspRatio = value
        End Set
    End
Property

    Property
LookupDir() As String
        Get
            Return
DirEnd Get
        Set
(ByVal value As String)
            Dir = value
        End Set
    End
Property

    Property
AutoTime() As Integer
        Get
            Return
Time
        End Get
        Set
(ByVal value As Integer)
            Time = value
        End Set
    End
Property

    Property
ShowStatusBar() As Boolean
        Get
            Return
statusBar
        End Get
        Set
(ByVal value As Boolean)
            statusBar = value
        End Set
    End
Property

    Sub
New()
        aspRatio = 1.3333
        Dir =
"C:\VBAppDirectory"
        Time = 30
        statusBar =
False
        Console.WriteLine("Creating a default settings.")
        If File.Exists(fileName) Then
            Dim
binReader As New BinaryReader(File.Open(fileName, FileMode.Open))
            Try
                Dim
testArray As Byte() = {0, 0, 0, 0}
                Dim count As Integer = binReader.Read(testArray, 0, 3)
                If count <> 0 Then
                    aspRatio = binReader.ReadSingle()
                    Dir = binReader.ReadString()
                    Time = binReader.ReadInt32()
                    statusBar = binReader.ReadBoolean()
                    Return
                End
If
            Catch
ex As EndOfStreamException
                Console
.WriteLine("{0} caught and ignored. " & "Using default values.", ex.GetType().Name)
            Finally
                binReader.Close()
            End Try
        End
If
    End
Sub

    Sub
Close()
        Dim binWriter As New BinaryWriter(File.Open(fileName, FileMode.Create))
        Console.WriteLine("Create a file and store the settings...")
        Try
            binWriter.Write(aspRatio)
            binWriter.Write(Dir)
            binWriter.Write(Time)
            binWriter.Write(statusBar)
        Finally
            binWriter.Close()
        End Try
    End
Sub
End
Class

OUTPUT

1.gif
 

Conclusion

Hope this article would have helped you in understanding BinaryReader and BinaryWriter Classes in VB.NET.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.