我有以下System.Collections.Specialized.NameValueCollection实例:

Dim UserSelection As New System.Collections.Specialized.NameValueCollection

UserSelection.Add("D_Color1", "Black")
UserSelection.Add("D_Color2", "Green")
UserSelection.Add("D_Color3", "Purple")

我需要将此实例保存到硬盘上,然后再将其作为实例从硬盘上装回。我怎么做?

最佳答案

使用BinaryFormatter

Using fs As New FileStream("DataFile.dat", FileMode.Create)
   Dim formatter As New BinaryFormatter
   formatter.Serialize(fs, UserSelection)
End Using

并反序列化
Using fs As New FileStream("DataFile.dat", FileMode.Open)
    Dim formatter As New BinaryFormatter
    UserSelection = DirectCast(formatter.Deserialize(fs), NameValueCollection)
End Using

10-06 05:53