在.NET Windows应用程序中存储用户设置的首选方法是什么?

时间:2021-12-11 16:57:24

Pardon my ignorance, but I've never really developed Windows applications. How do you store user settings? Is an embedded database the preferred method?

请原谅我的无知,但我从未真正开发过Windows应用程序。你如何存储用户设置?嵌入式数据库是首选方法吗?

4 个解决方案

#1


I think you are looking for user settings:

我认为您正在寻找用户设置:

The .NET Framework 2.0 allows you to create and access values that are persisted between application execution sessions. These values are called settings. Settings can represent user preferences, or valuable information the application needs to use. For example, you might create a series of settings that store user preferences for the color scheme of an application. Or you might store the connection string that specifies a database that your application uses. Settings allow you to both persist information that is critical to the application outside of the code, and to create profiles that store the preferences of individual users.

.NET Framework 2.0允许您创建和访问应用程序执行会话之间持久存储的值。这些值称为设置。设置可以表示用户首选项或应用程序需要使用的有价值信息。例如,您可以创建一系列设置来存储应用程序颜色方案的用户首选项。或者,您可以存储指定应用程序使用的数据库的连接字符串。设置允许您在代码之外保留对应用程序至关重要的信息,并创建存储各个用户首选项的配置文件。

#2


It depends on what kind of settings. There are a variety of methods from embedded databases (like SQLite) to XML files, to the Registry.

这取决于什么样的设置。从嵌入式数据库(如SQLite)到XML文件,再到Registry,有各种各样的方法。

  • If the settings are very few, the registry often makes sense.
  • 如果设置很少,注册表通常是有意义的。

  • If the settings are more complicated, and need to be hand edited, you can use XML files or JSON.
  • 如果设置更复杂,并且需要手动编辑,则可以使用XML文件或JSON。

  • If the settings are complex and do not need hand editing, an embedded database like SQLite, .NetBtree, or BerkelyDB .NET are good choices.
  • 如果设置很复杂并且不需要手动编辑,那么像SQLite,.NetBtree或BerkelyDB .NET这样的嵌入式数据库是不错的选择。

#3


Use Blane's Pegasus Library (http://pegasus.codeplex.com/).

使用Blane的Pegasus Library(http://pegasus.codeplex.com/)。

You could use his XmlSerializationHelper class which makes it a snap to turn objects into XML and vice versa.

您可以使用他的XmlSerializationHelper类,它可以快速将对象转换为XML,反之亦然。

Or you could use Isolated Storage (I would provide a link to MSDN if I wasn't a new user and restricted to one hyperlink per post). If you use IsolatedStorage, consider using Blane's IsolatedStorageHashtable class.

或者您可以使用独立存储(如果我不是新用户,我会提供指向MSDN的链接,每个帖子限制为一个超链接)。如果您使用IsolatedStorage,请考虑使用Blane的IsolatedStorageHashtable类。

#4


It all depends on what size of an application you are building. If you are on something simple, let's say, "family shopping list", you can store the settings in a good old plain text file.

这完全取决于您正在构建的应用程序的大小。如果您使用简单的东西,比如“家庭购物清单”,您可以将设置存储在一个好的旧纯文本文件中。

If you are building something bigger, for example a "classmate notifier" you can use an XML file, or some kind of other resource.

如果要构建更大的东西,例如“同学通知程序”,则可以使用XML文件或某种其他资源。

For any bigger application you should use some kind of relational database, for storing user data.

对于任何更大的应用程序,您应该使用某种关系数据库来存储用户数据。

#1


I think you are looking for user settings:

我认为您正在寻找用户设置:

The .NET Framework 2.0 allows you to create and access values that are persisted between application execution sessions. These values are called settings. Settings can represent user preferences, or valuable information the application needs to use. For example, you might create a series of settings that store user preferences for the color scheme of an application. Or you might store the connection string that specifies a database that your application uses. Settings allow you to both persist information that is critical to the application outside of the code, and to create profiles that store the preferences of individual users.

.NET Framework 2.0允许您创建和访问应用程序执行会话之间持久存储的值。这些值称为设置。设置可以表示用户首选项或应用程序需要使用的有价值信息。例如,您可以创建一系列设置来存储应用程序颜色方案的用户首选项。或者,您可以存储指定应用程序使用的数据库的连接字符串。设置允许您在代码之外保留对应用程序至关重要的信息,并创建存储各个用户首选项的配置文件。

#2


It depends on what kind of settings. There are a variety of methods from embedded databases (like SQLite) to XML files, to the Registry.

这取决于什么样的设置。从嵌入式数据库(如SQLite)到XML文件,再到Registry,有各种各样的方法。

  • If the settings are very few, the registry often makes sense.
  • 如果设置很少,注册表通常是有意义的。

  • If the settings are more complicated, and need to be hand edited, you can use XML files or JSON.
  • 如果设置更复杂,并且需要手动编辑,则可以使用XML文件或JSON。

  • If the settings are complex and do not need hand editing, an embedded database like SQLite, .NetBtree, or BerkelyDB .NET are good choices.
  • 如果设置很复杂并且不需要手动编辑,那么像SQLite,.NetBtree或BerkelyDB .NET这样的嵌入式数据库是不错的选择。

#3


Use Blane's Pegasus Library (http://pegasus.codeplex.com/).

使用Blane的Pegasus Library(http://pegasus.codeplex.com/)。

You could use his XmlSerializationHelper class which makes it a snap to turn objects into XML and vice versa.

您可以使用他的XmlSerializationHelper类,它可以快速将对象转换为XML,反之亦然。

Or you could use Isolated Storage (I would provide a link to MSDN if I wasn't a new user and restricted to one hyperlink per post). If you use IsolatedStorage, consider using Blane's IsolatedStorageHashtable class.

或者您可以使用独立存储(如果我不是新用户,我会提供指向MSDN的链接,每个帖子限制为一个超链接)。如果您使用IsolatedStorage,请考虑使用Blane的IsolatedStorageHashtable类。

#4


It all depends on what size of an application you are building. If you are on something simple, let's say, "family shopping list", you can store the settings in a good old plain text file.

这完全取决于您正在构建的应用程序的大小。如果您使用简单的东西,比如“家庭购物清单”,您可以将设置存储在一个好的旧纯文本文件中。

If you are building something bigger, for example a "classmate notifier" you can use an XML file, or some kind of other resource.

如果要构建更大的东西,例如“同学通知程序”,则可以使用XML文件或某种其他资源。

For any bigger application you should use some kind of relational database, for storing user data.

对于任何更大的应用程序,您应该使用某种关系数据库来存储用户数据。