VB.NET:保存并保存加载数据?

发布于 2024-10-10 12:40:59 字数 95 浏览 5 评论 0原文

假设我有一个包含一些重要信息的字典类型对象。 好吧,我的用户想要关闭该程序并稍后再处理它。

如何将字典和其他变量保存在一个文件中,该文件只能由我的程序本身读取?

Let's say I got a Dictionary type object holding some important info.
Well, my user wants to close the program and work on it later.

How do I save the Dictionary and other variables in one single file, which can only be read by my program itself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清晨说晚安 2024-10-17 12:41:00

如果您只讨论相对少量的数据,那么将其序列化为 XML 可能很容易。请参阅 http://msdn.microsoft.com/ en-us/library/system.xml.serialization.xmlserializer.serialize.aspx

基本上,您为序列化程序提供一个对象,它将获取其所有属性和内容并将它们转换为 XML。

从那里,您可以对其进行加密(请参阅 http://www.codeproject.com/KB/ security/SimpleEncryption.aspx)并将其写入文件。

不过要小心。 .NET 程序很容易反汇编,并且可以找到您的加密密钥。

If you are only talking about a relatively small amount of data, it is probably easy to just serialize it to XML. See http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.serialize.aspx

Basically, you give the serializer an object, and it will take all of its properties and what and convert them to XML.

From there, you can encrypt it (see http://www.codeproject.com/KB/security/SimpleEncryption.aspx) and write it to a file.

Be careful though. .NET programs are easily disassembled, and your encryption key can be found.

雄赳赳气昂昂 2024-10-17 12:40:59

如果您正在寻找简单的方法,我有两个词给您: 二进制序列化

.NET Framework 中内置的序列化功能对于此类任务非常有用,您只需将程序的一些实例数据吐出到磁盘,而不必担心自己编写一堆代码。

由于您不担心遵守任何类型的开放或标准数据格式,因此 XML 序列化没有任何好处。只需选择一个用于您的应用程序的自定义文件扩展名(最好是尚未被很多其他人使用的文件扩展名 - 请记住,您不必将自己限制为只有 3 个字符),然后将该文件扩展名与您的应用程序关联起来Windows 外壳。

当然,这不会对数据进行任何加密。有人仍然可以在记事本中打开您的数据文件并查看其内容,但它们不会很漂亮。它不是人类可读的数据格式,但也不一定安全。您的问题对于是否需要这样做不明确。

编辑:我看到您在评论中添加了您希望能够加密数据的评论。没问题。这个问题的公认答案提出了一种与二进制序列化结合使用的方法: 如何在序列化后加密和保存二进制流并将其读回? 本质上,您需要使用 CryptoStream 处理加密/解密。

If you're looking for simple, I have two words for you: binary serialization.

The serialization capabilities built into the .NET Framework are awesome for tasks like this, where you just want to spit some of your program's instance data out to disk without worrying about writing a bunch of code yourself.

Since you're not worried about adhering to any kind of an open or standard data format, there's no benefit to XML serialization. Just choose a custom file extension to use for your application (preferably one that's not already in use by very many others—remember you don't necessarily have to limit yourself to just 3 characters), and then associate that file extension with your application in the Windows shell.

Of course, this won't do anything to encrypt the data. Someone could still open up your data file in Notepad and see its contents, but they won't be very pretty. It's not a human-readable data format, but it's not necessarily secure either. Your question is ambiguous on whether or not this is required.

EDIT: I see you've added in a comment that you'd prefer to be able to encrypt your data. No problem. The accepted answer to this question suggests a way to do that in conjunction with binary serialization: How to encrypt and save a binary stream after serialization and read it back? Essentially, you'll want to use the CryptoStream class to handle the encryption/decryption.

御守 2024-10-17 12:40:59

大约有一百万零一种方法可以做到这一点。很大程度上取决于你想要什么以及你想走多远。这里的关键部分是“仅由我的程序读取”。如果您不希望其他人读取它,您很可能需要想出某种文件格式或加密机制。

There are about a million and one ways to do this. A lot depends on what you want and how far you want to go. The key piece here is "only read by my program". More than likely you will need to come up with some file format, or encryption mechanism if you don't want others to read it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文