.net - 如何将 System.Collections.Specialized.NameValueCollection 实例保存到硬盘并将其作为实例加载回来?

标签 .net vb.net visual-studio-2010

我有以下 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

关于.net - 如何将 System.Collections.Specialized.NameValueCollection 实例保存到硬盘并将其作为实例加载回来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6473477/

相关文章:

c# - 以不区分大小写的方式查找子字符串 - C#

.net - 如何设置相对于项目文件的资源文件路径?

visual-studio-2010 - Visual Studio 2010 中是否有键盘快捷键可以在方法之间移动光标?

vb.net - 如何在VB.NET中键入文字二进制?

.net - 如何汇总T的多个IEnumerables

c# - Math.Round 在一台客户电脑上的奇怪行为

visual-studio-2010 - VS2010在调试时将数组/集合数据保存到文件中

c# - 将 DOM XmlElement 反序列化为 .NET 对象

.net - 从 .NET 与 SQLite 对话的最佳/最简单方法是什么?

.net - 如何在 .NET 中使用 XSLT?