VB.NET Binary File Read Sequential – Source Code Example

Pluralsight courses
LinkedIn Premium uses PayScale to estimate job salaries

Premium (Not Free) Video Tutorials

Free Video Tutorials & Free Tools

VB.NET Binary File Read Sequential – Source Code Example

Purpose: – Illustrates the for .

View Other VB.NET Source Code Examples


  1. Install Visual Basic (Express or Standard Edition)
  2. Install SQL Server Express
  3. Download Northwind and pubs Database
  4. Attach Northwind Database to Databases in Sql Express
  5. Attach pubs Database to Databases in Sql Express


  • Console Application is used to simplify things, but Windows Forms or Web Forms could also be used
  • You can build a library of syntax examples by using same project over and over and just commenting out what you do not want to execute in Module1.vb


  1. Use Visual Basic 2008 Express or Standard Edition
  2. Create new project;
    • Click File/New Project
    • Select Console Application Template
    • Select Visual Basic for Language
    • name of project could be VBNET_Syntax.
  3. VB Syntax File Directory Binary File Read Sequential

  4. Add New folder named "FileDirectory"
    • Right-click project name in solution explorer;
    • add new folder;
    • name of folder could be: FileDirectory
  5. Add Class Named clsBinaryFileReadSequential to FileDirectory folder
    • Right-click FileDirectory folder;
    • add new item;
    • Select class
    • Class name could be clsBinaryFileReadSequential
  6. Click on copy code in code below to copy code into clsBinaryFileReadSequential.vb
  7. Click on copy code in second set of code below to copy code into Module1.vb
  8. Click green arrow or press F5 to run program

Step 1: Use Copy Code to Cut-n-paste code into clsBinaryFileReadSequential.vb

 Visual Basic |  copy code |? 
Imports System.IO
Imports System.Collections
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Public Class clsBinaryFileReadSequential
    Public Shared Sub Main()
        Dim formatter As BinaryFormatter = New BinaryFormatter()
        Dim output As FileStream
        Dim fileName As String = "books.dat"
            output = New FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write)
            Dim Book As Book = New Book(1, "The Republic", "Plato")
            formatter.Serialize(output, Book)
            Book = New Book(2, "Romeo & Juliet", "Shakespeare")
            formatter.Serialize(output, Book)
            Book = New Book(3, "The Symposium", "Plato")
            formatter.Serialize(output, Book)
        Catch fileException As FileNotFoundException
            Console.WriteLine("File Does Not Exits")
        Catch serializableException As SerializationException
            Console.WriteLine("Error Writing to File")
        Catch formattingException As FormatException
            Console.WriteLine("Invalid Format")
        Catch e As IOException
            Console.WriteLine("Cannot close file")
        End Try
        Dim input As FileStream
        Dim reader As BinaryFormatter = New BinaryFormatter()
        input = New FileStream(fileName, FileMode.Open, FileAccess.Read)
        Dim intFileLength As Integer = input.Length
        While input.Position < intFileLength
                Dim myBook As Book = CType(reader.Deserialize(input), Book)
            Catch serializableException As SerializationException
                Console.WriteLine("Serialization Exception")
            End Try
        End While
        Console.WriteLine("No more records in file")
    End Sub
End Class
<Serializable()> Public Structure Book
    Public ID As Integer
    <vbfixedstring (25)> Public BookName As String
    </vbfixedstring><vbfixedstring (25)> Public BookAuthor As String
    Public Sub New(ByVal new_id As Integer, ByVal Book_name As String, _
     ByVal Book_author As String)
        ID = new_id
        BookName = Book_name
        BookAuthor = Book_author
    End Sub
    Public Overrides Function ToString() As String
        Return ID & ": " & BookName & " " & BookAuthor
    End Function
End Structure

Step 2: Use View Plain to Cut-n-paste code into Module1.vb
 Visual Basic |  copy code |? 
Module Module1
    Sub Main()
        '***** DataBaseADONET *************
        'Dim mySQLConnectionConnectionString As New clsSQLConnectionConnectionString
        'Dim mySQLInsert As New clsSQLInsert
        'Dim mySQLParameters As New clsSQLParameters
        'Dim mySQLCallStoredProcedure As New clsSQLCallStoredProcedure
        'Dim mySQLSelect As New clsSQLSelect
        'Dim mySqlCommandDelete As New clsSqlCommandDelete
        'Dim mySqlCommandUpdate As New clsSqlCommandUpdate
        'Dim mySqlDataReader As New clsSqlDataReader
        'Dim myStringFormatDateTime As New clsStringFormatDateTime
        'Dim myStringBuilder As New clsStringBuilder
        'Dim myString As New clsString
        'Dim myDataTableSqlDataReader As New clsDataTableSqlDataReader
        'Dim myBinaryFileReadRandom As New clsBinaryFileReadRandom
        Dim myBinaryFileReadSequential As New clsBinaryFileReadSequential
    End Sub
End Module

Related posts:

  1. VB.NET Binary File Read Random – Source Code Example VB.NET Binary File Read Random - Source Code Example...
  2. VB.NET Sql Command Update Statement Source Code Example VB.NET Source Code Example shows how to use Sql Command...
  3. VB.NET Sql Command Delete Statement Source Code Example Example illustrates using VB.NET Sql Command Delete Statement and checking...
  4. VB.NET StringBuilder Source Code Example VB.NET StringBuilder Source Code Example...
  5. VB.NET String Format DateTime Source Code Example VB.NET String Format DateTime Source Code Example...

Related posts brought to you by Yet Another Related Posts Plugin.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

Powered by WP Hashcash

This blog uses the cross-linker plugin developed by Web-Developers.Net