Best way to save per user options in C#

穿精又带淫゛_ 提交于 2019-12-17 16:33:39

问题


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


回答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#

EDIT

Using Settings in 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.

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.




回答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.

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.




回答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.

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.

Check out the ConfigurationManager and SpecialFolders, that should get you started.



来源:https://stackoverflow.com/questions/744746/best-way-to-save-per-user-options-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!