Click here to Skip to main content
15,884,629 members
Articles / Programming Languages / Visual Basic
Article

Writing a custom Structure to a binary file in Visual Basic 2008

Rate me:
Please Sign up or sign in to vote.
4.08/5 (6 votes)
9 Mar 2008CPOL1 min read 61.5K   785   23   3
Techniques for writing custom data types written in Visual Basic 2008 to a binary file.

Introduction

In this article, I want to show you how to write the content of a Visual Basic structure into a binary file. This can be useful since the various methodologies exposed by the .NET Framework (like FileStream, BinaryWriter, and My objects) can write to binary files only data types that belong to the Base Class Library.

So, what is the solution? We can serialize the structure and write the serialized object to a binary file.

Using the code

First of all, open Visual Studio 2008 and create a new Visual Basic 2008 Console Application. We could have a very simple Structure called Software, which contains information about a computer program:

VB
Module Module1
    <serializable() /> Structure Software
        Dim programName As String
        Dim productionYear As Integer
        Dim programProducer As String
    End Structure

    Sub MakeBinaryFile()
        Dim Program As Software
        Program.programProducer = "Microsoft"
        Program.programName = "Windows Vista"
        Program.productionYear = 2006

The next step is to serialize the structure. We could implement a method to accomplish this. Here, I’ve specified a predetermined file name, but this is not often the real case. Comments inside the following code should be helpful:

VB
'Retrieves a BinaryFormatter object and
'instantiates a new Memorystream to which
'associates the above BinaryFormatter, then writes
'the content to a binary file via My namespace
Dim BF As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim MS As New System.IO.MemoryStream()

'Serialization is first in memory,
'then written to the binary file
BF.Serialize(MS, Program)
My.Computer.FileSystem.WriteAllBytes("c:\temp\programs.bin", MS.GetBuffer(), False)

Finally, we can read back our binary file to check that everything works correctly. The following code snippet accomplishes this:

VB
    'Verifies that deserializing works fine and shows
    'the result in the Console window.
    Dim bytes As Byte() = My.Computer.FileSystem.ReadAllBytes("c:\temp\programmi.bin")
    Program = DirectCast(BF.Deserialize(New System.IO.MemoryStream(bytes)), Software)
    Console.WriteLine(Program.programName + " produced by " + _
                      Program.programProducer + " in the Year " + _
                      Program.productionYear.ToString)
    Console.ReadLine()
End Sub

We just need to call our method from within the Sub Main, in the following way:

VB
Sub Main()
    MakeBinaryFile()
End Sub

Points of interest

For a simple structure like the one above, the code can appear not very useful. But you must think that structures can contain .NET objects of any kind (e.g.: streams).

Moreover, objects serialization offers good levels of performance in reading/writing.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Other
Italy Italy
I'm a Microsoft Visual Basic MVP. I'm an Italian .NET developer and I write articles and books about Visual Basic, Visual Studio LightSwitch, and the .NET technologies.

Check out my blog at: http://community.visual-basic.it/AlessandroEnglish

Comments and Discussions

 
Questionhow to encrypt the result file? Pin
sseerraajj3-Feb-10 19:40
sseerraajj3-Feb-10 19:40 
GeneralThanks a lot !!!!!!!!!!!! Pin
Damon8810-Dec-09 6:12
Damon8810-Dec-09 6:12 
GeneralThe web page has caused you to loose your XML tag in the structure Pin
not_starman9-Mar-08 15:43
not_starman9-Mar-08 15:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.