我需要将程序中的一个对象(该对象存储数据)保存到硬盘驱动器,以便下次程序启动时加载它
我曾尝试使用序列化和 xml 文件输出,但我似乎无法使其正常工作,因为我拥有的数据不是“字符串”对象类型。
我考虑过使用文件打开/放置/获取,但 MSDN 建议不要这样做,因为它比序列化效率低得多。
任何可以实现我的目标的简单加载/保存功能?
在此先感谢马丁
我需要将程序中的一个对象(该对象存储数据)保存到硬盘驱动器,以便下次程序启动时加载它
我曾尝试使用序列化和 xml 文件输出,但我似乎无法使其正常工作,因为我拥有的数据不是“字符串”对象类型。
我考虑过使用文件打开/放置/获取,但 MSDN 建议不要这样做,因为它比序列化效率低得多。
任何可以实现我的目标的简单加载/保存功能?
在此先感谢马丁
我发现我需要在序列化之前将对象转换为二进制数据。
对于其他人,这是我的功能
'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