Visual Basic Code Snippet - Load object from binary file

Visual Basic Code Snippet - Load object from binary file

Visual Basic Code Snippet - Load object from binary file

(VB) Visual Basic code snippet open an external binary data file and convert it to object. This function can be use to deserialize those byte array data (Serialized) from external file to their original objects.

Bookmark:

Visual Basic Code Snippet - Load object from binary file

This .Net Visual Basic code snippet open an external binary data file and convert it to object. This function can be use to deserialize those byte array data (Serialized) from external file to their original objects like image data ..etc. This function uses System.IO and System.Runtime.Serialization.Formatters.Binary name spaces to convert binary data file to object. This function useful to implement database cache control system for high traffic databases by storing cached query object data in external files and read them back to database objects. Modify the exception handling section as to your project requirements.

Note that it will be necessary to cast the returned generic Object from the deserialization method back into the original type of object you passed in to the serialization method originally.

''' <summary>
''' Function to get object from external file
''' </summary>
''' <param name="_FileName">File name to get object</param>
''' <returns>object</returns>
Public Function FileToObject(ByVal _FileName As String) As Object
    Try
        ' Open file for reading
        Dim _FileStream As New System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)

        ' attach filestream to binary reader
        Dim _BinaryReader As New System.IO.BinaryReader(_FileStream)

        ' get total byte length of the file
        Dim _TotalBytes As Long = New System.IO.FileInfo(_FileName).Length

        ' read entire file into buffer
        Dim _ByteArray() As Byte = _BinaryReader.ReadBytes(CInt(Fix(_TotalBytes)))

        ' close file reader and do some cleanup
        _FileStream.Close()
        _FileStream.Dispose()
        _FileStream = Nothing
        _BinaryReader.Close()

        ' convert byte array to memory stream
        Dim _MemoryStream As New System.IO.MemoryStream(_ByteArray)

        ' create new BinaryFormatter
        Dim _BinaryFormatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()

        ' set memory stream position to starting point
        _MemoryStream.Position = 0

        ' Deserializes a stream into an object graph and return as a object.
        Return _BinaryFormatter.Deserialize(_MemoryStream)
    Catch _Exception As Exception
        ' Error
        Console.WriteLine("Exception caught in process: {0}", _Exception.ToString())
    End Try

    ' Error occured, return null
    Return Nothing
End Function


Here is a simple example showing how to use above function (FileToObject). In this example we read binary data from external file (image-object.dat) and convert byte array into object, in this case it's an image object, and show the image in picturebox.

pictureBox2.Image = CType(FileToObject("c:\image-object.dat"), Image)


VB Keywords Used:

  • FileStream
  • BinaryReader
  • FileInfo
  • FileMode
  • FileAccess
  • MemoryStream
  • BinaryFormatter
  • Deserialize
  • byte
  • Exception

Code Snippet Information:

  • Applies To: Visual Studio, .Net, VB, Visual Basic, CLI, Byte Array, File to object, FileStream, BinaryReader, FileMode, BinaryFormatter, Deserialize, Cache control, Load objects from file
  • Programming Language : Visual Basic (VB)

External Resources:

Leave a comment