Objective
In WinForm projects, you often need to read the app. config file. Such as:
1 var version = system.configuration.configurationmanager.appsettings["version "];
and "write", before thought of course is this:
1 ConfigurationManager.AppSettings.Set ("version","1.0.0 ");
But this writing did not succeed, do not understand why. At that time, I thought that this app. config is not allowed to write operations. For configuration information modification requirements, it can only be implemented by reading and writing XML files. I don't know, have you ever met.
Today on the internet accidentally found a way to write the app. Config, the code is as follows:
1 Private voidSetappsettingsvalue (stringKeystringvalue)2 {3 stringFile =System.Windows.Forms.Application.ExecutablePath;4Configuration config =configurationmanager.openexeconfiguration (file); 5 //determine if a node is included6 if(config. AppSettings.Settings.AllKeys.Contains (key))7 {8Config. Appsettings.settings[key]. Value =value;9 }Ten Else One { A //Adding nodes - CONFIG. APPSETTINGS.SETTINGS.ADD (key, value); - } the CONFIG. Save (configurationsavemode.modified); -Configurationmanager.refreshsection ("appSettings"); -}The effect is as follows:
OK, and get a wheel, hope can help everyone. Good night....
WinForm write the App. Config Profile--it Wheel series (eight)