Visual Basic Code Snippet - Save object to file

Visual Basic Code Snippet - Save object to file

Visual Basic Code Snippet - Save object to file

(VB) Visual Basic code snippet save object to external file. This function can be use to serialize objects into byte array and save them in external file.

Bookmark:

Visual Basic Code Snippet - Save object to file

This .Net Visual Basic code snippet save object to external file. This function can be use to serialize objects into byte array and save them in external file. This function uses System.IO and System.Runtime.Serialization.Formatters.Binary name spaces to save object to file. 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.

''' <summary>
''' Function to save object to external file
''' </summary>
''' <param name="_Object">object to save</param>
''' <param name="_FileName">File name to save object</param>
''' <returns>Return true if object save successfully, if not return false</returns>
Public Function ObjectToFile(ByVal _Object As Object, ByVal _FileName As String) As Boolean
    Try
        ' create new memory stream
        Dim _MemoryStream As New System.IO.MemoryStream()

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

        ' Serializes an object, or graph of connected objects, to the given stream.
        _BinaryFormatter.Serialize(_MemoryStream, _Object)

        ' convert stream to byte array
        Dim _ByteArray() As Byte = _MemoryStream.ToArray()

        ' Open file for reading
        Dim _FileStream As New System.IO.FileStream(_FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write)

        ' Writes a block of bytes to this stream using data from a byte array.
        _FileStream.Write(_ByteArray.ToArray(), 0, _ByteArray.Length)

        ' close file stream
        _FileStream.Close()

        ' cleanup
        _MemoryStream.Close()
        _MemoryStream.Dispose()
        _MemoryStream = Nothing
        _ByteArray = Nothing

        Return True
    Catch _Exception As Exception
        ' Error
        Console.WriteLine("Exception caught in process: {0}", _Exception.ToString())
    End Try

    ' Error occured, return null
    Return False
End Function


Here is a simple example showing how to use above function (ObjectToFile). This example illustrates how to save picturebox image object to external file.

ObjectToFile(pictureBox1.Image, "c:\\image-object.dat")


VB Keywords Used:

  • FileStream
  • FileMode
  • FileAccess
  • MemoryStream
  • BinaryFormatter
  • Serialize
  • byte
  • Exception

Code Snippet Information:

  • Applies To: Visual Studio, .Net, VB, Visual Basic, CLI, Byte Array, Object to file, FileStream, FileMode, BinaryFormatter, Serialize, Cache control, Save object to file
  • Programming Language : Visual Basic (VB)

External Resources:

Leave a comment