在Vb.net中保存和加载对象

在Vb.net中保存和加载对象,vb.net,Vb.net,我需要将程序中的一个对象(该对象存储数据)保存到硬盘,以便下次程序启动时加载它 我尝试过使用序列化和xml文件输出,但似乎无法实现,因为我拥有的数据不是“字符串”对象类型 我曾考虑过使用文件open/put/get,但MSDN建议不要这样做,因为它比序列化效率低得多 是否有任何简单的加载/保存功能可以实现我的目标 提前谢谢 Martin我发现在序列化之前需要将对象转换为二进制数据 对于其他人,这里是我的功能 'Imports Imports System.IO Imports System.Te

我需要将程序中的一个对象(该对象存储数据)保存到硬盘,以便下次程序启动时加载它

我尝试过使用序列化和xml文件输出,但似乎无法实现,因为我拥有的数据不是“字符串”对象类型

我曾考虑过使用文件open/put/get,但MSDN建议不要这样做,因为它比序列化效率低得多

是否有任何简单的加载/保存功能可以实现我的目标

提前谢谢
Martin

我发现在序列化之前需要将对象转换为二进制数据

对于其他人,这里是我的功能

'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

我发现我需要在序列化之前将对象转换为二进制数据

对于其他人,这里是我的功能

'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

序列化有什么问题?序列化有什么问题?