在 .NET Winforms 应用程序中保存设置

发布于 2024-09-05 10:35:48 字数 315 浏览 1 评论 0原文

可能的重复:
最好的方法是什么存储 .NET 应用程序的用户设置?

我发现了很多非常不同的示例,说明如何从 Winforms 应用程序保存应用程序设置(每个用户)。

我想在 C# 中执行此操作的正确方法非常简单,希望有人能启发我?

Possible Duplicate:
What is the best way to store user settings for a .NET application?

I have found alot of VERY different examples as to how one would save application settings (per user) from a Winforms application.

I imagine that the correct way to do this is very simple in c# and am hoping someone can enlighten me?

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

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

发布评论

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

评论(3

奈何桥上唱咆哮 2024-09-12 10:35:49

您可以使用.NET 提供的设置基础结构。在项目的属性页面中,转到“设置”页面,然后定义您的设置。您可以将每个设置的范围定义为“应用程序”或“用户”。将自动生成一个类来从代码访问这些设置。

要访问设置 Foo 和 Bar,请使用:

// Read settings
textBoxFoo.Text = Properties.Settings.Default.Foo;

// Write settings
Properties.Settings.Default.Bar = checkBoxBar.IsChecked;

// Save settings
Properties.Settings.Default.Save();

You can use the settings infrastructure provided by .NET. In your project's property pages, go to the Settings page, and define your settings. You can define the scope of each setting as "Application" or "User". A class will be automatically generated to access these settings from code.

To access settings Foo and Bar, use :

// Read settings
textBoxFoo.Text = Properties.Settings.Default.Foo;

// Write settings
Properties.Settings.Default.Bar = checkBoxBar.IsChecked;

// Save settings
Properties.Settings.Default.Save();
天气好吗我好吗 2024-09-12 10:35:49

我会使用应用程序设置。它非常简单,并且会为您解决一些问题(例如,在没有管理访问权限的情况下,没有对可能安装应用程序的文件夹的写入权限,这排除了直接使用 app.config 进行设置的情况)。

I would use Application Settings. It's pretty straightforward and will take care of some issues for you (such as not having write access to the folder where your app may be installed without administrative access, which rules out directly using app.config for your settings).

猫七 2024-09-12 10:35:48

在某些时候,答案归结为品味问题。我想说您最终至少会得到以下选项:

  • 将其存储在注册表中,其中您有 HKEY_CURRENT_USER 密钥。它下面的所有内容都是特定于用户的。当您想要存储一堆小的键值对时,这通常是首选。该系统的主要优点是您的设置很容易在多个不同的应用程序中查找和共享。要遵循此路径,您可以从此处。
  • 使用 .NET 应用程序设置,提供在运行时访问设置的最简单方法。同样,它更适合与小型数据的键值对一起使用。 IMO,这种方法的主要优点是它的简单性,以及它使您能够使用一些 .NET 类作为值(不强迫您将所有内容转换为更基本的类型)。要遵循此路径,您可以从 此处
  • 将其存储在“用户数据”文件夹中,该文件夹通常隐藏在用户的个人资料目录下。当您想要存储大量数据或任意数量的文件时,这是首选。此方法的主要优点是您可以像处理任何文件一样操作数据(这也可能是一个缺点)。要遵循此路径,您可以从 此处开始

At some point, the answer boils down to a matter of taste. I'd say you'll end up with at least these options:

  • store it in the registry, where you have the HKEY_CURRENT_USER key. Everything under it is user-specific. This is usually preferred when you want to store a bunch of small key-value pairs. The main advantage of this system is that your settings are easy to find and share throughout several different applications. To follow this path, you can start from here.
  • using .NET Application settings, provides the easiest way to access your settings at runtime. Again, it's better for using with key-value pairs of small-sized data. IMO, the main advantages of this method is its simplicity and the fact that it empowers you to use some .NET classes as values (not forcing you to convert everything into more basic types). To follow this path, you can start from here.
  • store it in User Data folders, which are usually hidden under the user's profile directory. This is preferred when you want to store a large amount of data or any number of files. The main advantage of this method is that you can manipulate your data as you would with any files (that may also be a disadvantage). To follow this path, you can start from here.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文