How to update appSettings in a WPF app?

牧云@^-^@ 提交于 2020-01-12 14:32:11

问题


Greetings to all! This is my first question here on stackoverflow. I have a WPF application that I am writing for the fellow developers in my department, and there are a couple of settings that I need to check for at startup and update if they are not set (one is the location of an executable on the users computer, we all have it, just not in the same place). So when my app starts up for the first time, I need to pop a filechooser to have them select the location.

What I need to do is write the location of that to the appSettings, but I just can't seem to get it, and I searched Google pretty hard last night trying to find a way to do it. Most answers I saw involved reading the app.config file as straight XML, and that doesn't seem right.

So, I just need a way to update values in the appSettings for my application. I can read from them just fine, just haven't figured out how to write to them. Thanks so much!

James


回答1:


Have you looked into the ConfigurationManager class? It provides a more robust interface to the app.config file and you can do something like this:

Configuration oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
oConfig.AppSettings.Settings["PreferenceToRemember"].Value = “NewValue”;
oConfig.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection(”appSettings”);

Just remember to import System.Configuration into your project. It isn't added by default.




回答2:


Take a look at the Configuration class and Enterprise Library. You can find detailed instructions here.



来源:https://stackoverflow.com/questions/305529/how-to-update-appsettings-in-a-wpf-app

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