Reputation: 625
I need to save an object that I have in my program (this object stores data) to the hard drive so i can load it next time the program starts
I have tried using serialization and xml file output but I cant seem to get this working since the data I have is not of the 'string' object type.
I considered using file open/put/get but MSDN recommends against this since it is much more inefficient than serialization.
Any simple load/save functions that will accomplish my goal?
Thanks in advance Martin
Upvotes: 0
Views: 6194
Reputation: 625
I figured out that I needed to convert the object to binary data before serialization.
For others, here are my functions
'Imports
Imports System.IO
Imports System.Text
Imports System.Collections
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
'Functions
Public Function Load()
If My.Computer.FileSystem.FileExists(mstrSaveFile) Then
Dim fs As Stream = New FileStream(mstrSaveFile, FileMode.Open)
Dim bf As BinaryFormatter = New BinaryFormatter()
mstrData = CType(bf.Deserialize(fs), CType(mstrData))
fs.Close()
End If
Return True
End Function
Public Function Save()
If My.Computer.FileSystem.FileExists(mstrSaveFile) = True Then
My.Computer.FileSystem.DeleteFile(mstrSaveFile)
End If
Dim fs As Stream = New FileStream(mstrSaveFile, FileMode.Create)
Dim bf As BinaryFormatter = New BinaryFormatter()
bf.Serialize(fs, mstrData)
fs.Close()
Return True
End Function
Upvotes: 4