Posted By

chavcho on 03/10/09


Tagged

object serialize


Versions (?)

Serialize an object and write it to the disk


 / Published in: VB.NET
 

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)

  1. 'serialize an object to a file
  2. Public Shared Sub SerializeToFile(ByVal path As String, ByVal data As Object)
  3. Try
  4. Dim BF As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
  5. Dim MS As New System.IO.MemoryStream()
  6. BF.Serialize(MS, data)
  7. My.Computer.FileSystem.WriteAllBytes(path, MS.GetBuffer(), False)
  8. Catch ex As Exception
  9. Throw ex
  10. End Try
  11. End Sub

Report this snippet  

You need to login to post a comment.