在 C# 中保存每个用户选项的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/744746/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:31:23  来源:igfitidea点击:

Best way to save per user options in C#

c#settings

提问by

What is an easy way to save/load settings? I'd prefer it if each user has their own set of settings.

保存/加载设置的简单方法是什么?如果每个用户都有自己的设置,我更喜欢它。

采纳答案by Samuel

If this is a desktop application, the best way to store application and/or user specific settings is to use the builtin methods. Using Settings in C#

如果这是一个桌面应用程序,存储应用程序和/或用户特定设置的最佳方法是使用内置方法。在 C# 中使用设置

EDIT

编辑

Using Settings in C# Visual Studio 2010

在 C# Visual Studio 2010 中使用设置

回答by Jason Coyne

This would depend a lot on the type of application.

这在很大程度上取决于应用程序的类型。

If this is a desktop application you could save information into the registry in the user's area. Or into their User directory on disk. If this is a web or server application, you would need to store it into a database keyed by user, or to a disk file named for each user or something.

如果这是一个桌面应用程序,您可以将信息保存到用户区域的注册表中。或者进入他们在磁盘上的用户目录。如果这是一个 web 或服务器应用程序,你需要将它存储到一个由用户键控的数据库中,或者存储到为每个用户命名的磁盘文件或其他东西。

Since you mention options, it seems like the client path is more likely.

由于您提到了选项,因此客户端路径似乎更有可能。

回答by jle

ConfigurationManager works pretty well (desktop application). In essence, you are using the app.config xml file to save settings. For most basic use, just edit the app.config. Add a key value pair for each setting you would like to save... then to access it, just do a ConfigurationManager.get..["yourKey"] and it will return the setting. Setting it is pretty much the same.

ConfigurationManager 工作得很好(桌面应用程序)。实质上,您正在使用 app.config xml 文件来保存设置。对于最基本的使用,只需编辑 app.config。为您想要保存的每个设置添加一个键值对...然后访问它,只需执行 ConfigurationManager.get..["yourKey"] 它将返回设置。设置它几乎相同。

回答by Steve

In .Net applications create settings like this with the built in settings options in Visual Studio - the values are saved in a config file, but VS gives you a nice interface to create them and a .Net class to access them in a strongly typed way. You can create user specific settings or app specific settings in this way.

在 .Net 应用程序中,使用 Visual Studio 中的内置设置选项创建这样的设置 - 这些值保存在配置文件中,但 VS 为您提供了一个很好的界面来创建它们和一个 .Net 类来以强类型方式访问它们. 您可以通过这种方式创建用户特定设置或应用特定设置。

To create a settings file double click on the Properties folder then select the Settings tab, then click the link to create a "settings.settings" file (yeah, great name!). Once you do this you'll see the actual settings stored in app.config and the autogenerated code to get to these is in Settings.designer.cs.

要创建设置文件,请双击“属性”文件夹,然后选择“设置”选项卡,然后单击链接以创建“settings.settings”文件(是的,好名字!)。执行此操作后,您将看到存储在 app.config 中的实际设置,并且自动生成的代码位于 Settings.designer.cs 中。

回答by Chris

When you say, "each user has their own set of settings", are you talking about an app that is shared by colleagues via a network or an app on a shared computer?

当您说“每个用户都有自己的一组设置”时,您是指同事通过网络共享的应用程序还是共享计算机上的应用程序?

Personally, I would go a the settings file route. You can use the registry, but that makes me leery. There have been places in Windows (assuming you're on Windows) to store User Data for forever and a day, all you need to do is write to them. And if you're on Vista, writing to the registry may not be an option, unless you want to UAC prompt your users.

就个人而言,我会选择设置文件路线。您可以使用注册表,但这让我感到怀疑。Windows 中有一些地方(假设你在 Windows 上)可以永远存储用户数据一天,你需要做的就是写入它们。如果您使用的是 Vista,则写入注册表可能不是一种选择,除非您希望 UAC 提示您的用户。

If your app already uses a SQL back end of some kind, use that. Make a settings table, or a key/value bag and store it there. Serialize that into an object and vice verse and you should have a version 1 of a settings manager.

如果您的应用程序已经使用某种类型的 SQL 后端,请使用它。制作一个设置表或一个键/值包并将其存储在那里。将其序列化为一个对象,反之亦然,您应该有一个设置管理器的版本 1。

Check out the ConfigurationManagerand SpecialFolders, that should get you started.

查看ConfigurationManagerSpecialFolders,它们应该可以帮助您入门。