What is an easy way to save/load settings? I'd prefer it if each user has their own set of settings.
什么是保存/加载设置的简单方法?如果每个用户都有自己的设置,我更喜欢它。
5 个解决方案
#1
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中使用设置
#2
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.
由于您提到选项,似乎客户端路径更有可能。
#3
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”],它将返回该设置。设置它几乎是一样的。
#4
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.
要创建设置文件,请双击Properties文件夹,然后选择Settings选项卡,然后单击链接以创建“settings.settings”文件(是的,好名字!)。执行此操作后,您将看到存储在app.config中的实际设置以及要生成这些内容的自动生成的代码位于Settings.designer.cs中。
#5
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 ConfigurationManager and SpecialFolders, that should get you started.
查看ConfigurationManager和SpecialFolders,它们可以帮助您入门。
#1
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中使用设置
#2
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.
由于您提到选项,似乎客户端路径更有可能。
#3
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”],它将返回该设置。设置它几乎是一样的。
#4
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.
要创建设置文件,请双击Properties文件夹,然后选择Settings选项卡,然后单击链接以创建“settings.settings”文件(是的,好名字!)。执行此操作后,您将看到存储在app.config中的实际设置以及要生成这些内容的自动生成的代码位于Settings.designer.cs中。
#5
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 ConfigurationManager and SpecialFolders, that should get you started.
查看ConfigurationManager和SpecialFolders,它们可以帮助您入门。