Serialize an object and write it to the disk

/ Published in: VB.NET
Save to your folder(s)

Serializes any object and writes it to the specified location on the disk.


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)

Copy this code and paste it in your HTML
  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


RSS Icon Subscribe to comments

You need to login to post a comment.