Return to Snippet

Revision: 12316
at March 10, 2009 10:30 by chavcho


Initial Code
'serialize an object to a file 
    Public Shared Sub SerializeToFile(ByVal path As String, ByVal data As Object)
        Try
            Dim BF As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
            Dim MS As New System.IO.MemoryStream()
            BF.Serialize(MS, data)
            My.Computer.FileSystem.WriteAllBytes(path, MS.GetBuffer(), False)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

Initial URL


Initial Description
Serializes any <serializable/> object and writes it to the specified location on the disk.

Example:


Dim obj As DataTable
'save obj to disk
SerializeToFile(filepath, obj)
'to read it back, use DirectCast
Dim obj2 As DataTable = DirectCast(My.Computer.FileSystem.ReadAllBytes(filepath), DataTable)

Initial Title
Serialize an object and write it to the disk

Initial Tags
object

Initial Language
VB.NET