C# .NET - 存储一些非常小规模的持久信息的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10323755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# .NET - method to store some very small scale persistent information?
提问by John Humphreys - w00te
I have an application that will need extremely little persistent storage. Realistically, we're talking about < 30 integers. All the application needs is to know those integers on next startup (and the integers do change as it runs).
我有一个需要极少持久存储的应用程序。实际上,我们谈论的是 < 30 个整数。应用程序所需要的只是在下次启动时知道这些整数(并且这些整数在运行时会发生变化)。
A database is overkill for this, but I don't particularly want to just use a text file either.
数据库对此有点过分,但我也不想特别想只使用文本文件。
Does C# have any mechanism for persisting small values like this between runs? I've noticed you can store things in resource files and some other places - I don't know if you can change those in the runtime though. I'm just learning C# & .NET for a new job, so apologies if this is a silly question!
C# 是否有任何机制可以在运行之间保持这样的小值?我注意到您可以将内容存储在资源文件和其他一些地方 - 不过我不知道您是否可以在运行时更改这些内容。我只是为了一份新工作而学习 C# 和 .NET,如果这是一个愚蠢的问题,我深表歉意!
采纳答案by Zachary
Here is a blurp of another SOpost that explains how to setup Application Settings, this is a simple file based solution for reading/writing values.
这是另一篇 SO帖子的模糊说明,它解释了如何设置应用程序设置,这是一个用于读取/写入值的基于文件的简单解决方案。
"If you work with Visual Studio then it is pretty easy to get persistable settings. Right click on the project in Solution Explorer, choose Properties. Select the Settings tab, click on the hyperlink if settings doesn't exist.Use the Settings tab to create application settings. Visual Studio creates the files Settings.settings and Settings.Designer.settings that contain the singleton class Settings inherited from ApplicationSettingsBase. You can access this class from your code to read/write application settings:"
"如果您使用 Visual Studio,则很容易获得持久设置。在解决方案资源管理器中右键单击项目,选择属性。选择设置选项卡,如果设置不存在,请单击超链接。使用设置选项卡创建应用程序设置。Visual Studio 创建文件 Settings.settings 和 Settings.Designer.settings,其中包含从 ApplicationSettingsBase 继承的单例类 Settings。您可以从代码访问此类以读取/写入应用程序设置:“
Settings.Default["SomeProperty"] = "Some Value";
Settings.Default.Save(); // Saves settings in application configuration file
回答by Nesim Razon
I would use embeeded sqlite database.
我会使用嵌入的 sqlite 数据库。
http://www.codeproject.com/Articles/22165/Using-SQLite-in-your-C-Application
http://www.codeproject.com/Articles/22165/Using-SQLite-in-your-C-Application
SQLite is a small, fast and embeddable database where the database engine and the interface are combined into a single library. It also has the ability to store all the data in a single file. So if your application requires a standalone database, SQLite is perhaps the perfect choice for you. There are, of course, other reasons for choosing SQLite including:
SQLite 是一个小型、快速且可嵌入的数据库,其中数据库引擎和界面合并为一个库。它还能够将所有数据存储在一个文件中。因此,如果您的应用程序需要一个独立的数据库,SQLite 可能是您的最佳选择。当然,还有其他选择 SQLite 的原因,包括:
SQLite has a small memory footprint and only a single library is required to access databases, making it ideal for embedded database applications. SQLite has been ported to many platforms and runs even on Windows CE and Palm OS. SQLite is ACID-compliant, meeting all four criteria - Atomicity, Consistency, Isolation, and Durability. SQLite implements a large subset of the ANSI-92 SQL standard, including views, sub-queries and triggers. No problem of extra database drivers, ODBC configuration required. Just include the library and the data file with your application. SQLite has native language APIs for C/C++, PHP, Perl, Python, Tcl etc. Native API for C# is still not present.
SQLite 内存占用小,访问数据库只需要一个库,非常适合嵌入式数据库应用程序。SQLite 已被移植到许多平台,甚至可以在 Windows CE 和 Palm OS 上运行。SQLite 符合 ACID,满足所有四个标准 - 原子性、一致性、隔离性和持久性。SQLite 实现了 ANSI-92 SQL 标准的一个很大的子集,包括视图、子查询和触发器。额外的数据库驱动程序没有问题,需要 ODBC 配置。只需在您的应用程序中包含库和数据文件。SQLite 具有用于 C/C++、PHP、Perl、Python、Tcl 等的本机语言 API。用于 C# 的本机 API 仍然不存在。
回答by Steve Danner
You could just serialize the list to an xml file. This is how you save it:
您可以将列表序列化为 xml 文件。这是你保存它的方式:
var xs = new System.Xml.Serialization.XmlSerializer(typeof(List<int>));
List<int> ints = new List<int> { 1, 2, 3 };
using (FileStream fs = new FileStream(@"C:\store.xml", FileMode.OpenOrCreate))
{
xs.Serialize(fs, ints);
}
And retrieving it is equally easy:
检索它同样容易:
using (FileStream fs = new FileStream(@"C:\store.xml", FileMode.OpenOrCreate))
{
ints = xs.Deserialize(fs) as List<int>;
}
回答by balexandre
I would choose file storage and avoid database or registry as the first it needs to much of the framework and the second ... ohh well, never play with Registry :)
我会选择文件存储并避免数据库或注册表作为第一个它需要大部分框架和第二个......哦,好吧,永远不要玩注册表:)
using XML as a file storage this is a great read:
使用 XML 作为文件存储这是一个很好的阅读:
you can create your file to save all ID's in the same node, or write one ID per node, it's up to you, something like this would be fine:
您可以创建文件以将所有 ID 保存在同一节点中,或者为每个节点编写一个 ID,这取决于您,这样的事情就可以了:
<?xml version="1.0" encoding="utf-8" ?>
<settings>
<selected>
<id>1</id>
<id>8</id>
<id>12</id>
<id>15</id>
</selected>
</settings>
In order to create such file:
为了创建这样的文件:
XmlDocument doc = new XmlDocument();
XmlElement set = (XmlElement)doc.AppendChild(doc.CreateElement("settings"));
XmlElement sel = (XmlElement)set.AppendChild(doc.CreateElement("selected"));
int[] selectedIds = new int[] { 1, 2, 3, 4, 5 };
foreach (int id in selectedIds)
sel.AppendChild(doc.CreateElement("id")).InnerText = id.ToString();
string path = Path.GetDirectoryName(Application.ExecutablePath);
doc.Save(path + "/settings.xml");
In order to read you can use XmlReader, XmlTextReader, XDocument, etc...
为了读取,你可以使用XmlReader,XmlTextReader,XDocument等...
回答by oatsoda
If you're using c# 4.0 and upwards, you might want to have a look at the new Memory Mapped Files.
如果您使用的是 c# 4.0 及更高版本,您可能需要查看新的Memory Mapped Files。
Using Persisted Memory Mapped files will give you the ease of a file, but the speed of being mapped into memory. You would of course still have to manage the file format yourself.
使用持久内存映射文件会给你一个文件的方便,但被映射到内存的速度。当然,您仍然需要自己管理文件格式。
- Persisted memory-mapped files
Persisted files are memory-mapped files that are associated with a source file on a disk. When the last process has finished working with the file, the data is saved to the source file on the disk. These memory-mapped files are suitable for working with extremely large source files.
- 持久内存映射文件
持久化文件是与磁盘上的源文件相关联的内存映射文件。当最后一个进程完成对文件的处理后,数据将保存到磁盘上的源文件中。这些内存映射文件适用于处理极大的源文件。
This probably may be overkill for 30 integers, but it's another option, especially if speed is key.
这对于 30 个整数来说可能有点矫枉过正,但它是另一种选择,尤其是在速度是关键的情况下。

