For the appsetting node of the App. Config profile in C/s mode , the support configuration information is now available and can be persisted .
First, learn how to obtain the contents of the configuration information "Get configuration information Recommended Use this"
1.1 Get method One: You need to reference the namespace before: using System.Configuration;
configurationmanager.appsettings["key"]
1.2 Get method Two: Use the XML class, Load the configuration file directly, and then read the information under the appsetting node "deprecated"
Second, write, or modify configuration i information " recommended two methods to use together "
2.1 Method One: Use the ConfigurationManager class that comes with the system
There is a problem: after writing, can not persist , can only function in the interval that the program runs. When the program is closed, the stored information disappears.
Configuration CFA = configurationmanager.openexeconfiguration (configurationuserlevel.none); CFA. appsettings.settings["SMTP"]. Value = value;
2.2 Method Two: Use the XML class to load the configuration file, add the XmlNode node, or modify the "directly modify the original file"
There is a problem: change/Add end, not immediately use the latest configuration information.
Try{XmlDocument XDoc=NewXmlDocument (); //get the app. Config file absolute pathString BasePath =System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; String Path= Basepath.substring (0, Basepath.length-Ten) +"App."; Xdoc.load (path); XmlNode XNode; XmlElement xElem1; XmlElement xElem2; XNode= Xdoc.selectsinglenode ("//appsettings"); XElem1= (XmlElement) Xnode.selectsinglenode ("//add[@key = '"+ AppKey +"']"); if(XElem1! =NULL) Xelem1.setattribute ("value", Appvalue); Else{xElem2= Xdoc.createelement ("Add"); Xelem2.setattribute ("Key", AppKey); Xelem2.setattribute ("value", Appvalue); Xnode.appendchild (XELEM2); } xdoc.save (path); //Properties.Settings.Default.Reload (); } Catch(Exception e) {stringError =E.message; }
Recommended methods of Use:
Try{XmlDocument XDoc=NewXmlDocument (); //get the app. Config file absolute pathString BasePath =System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; BasePath= Basepath.substring (0, Basepath.length-Ten); String Path= BasePath +"App."; Xdoc.load (path); XmlNode XNode; XmlElement xElem1; XmlElement xElem2; //Modify the contents of the file, you also need to modify the contents of the cache, so that just after the modification can be used//If you do not modify the cache, you need to wait until the program is shut down before you can use the modified configuration informationConfiguration CFA =configurationmanager.openexeconfiguration (Configurationuserlevel.none); XNode= Xdoc.selectsinglenode ("//appsettings"); XElem1= (XmlElement) Xnode.selectsinglenode ("//add[@key = '"+ AppKey +"']"); if(XElem1! =NULL) {Xelem1.setattribute ("value", Appvalue); CfA. appsettings.settings["AppKey"]. Value =Appvalue; } Else{xElem2= Xdoc.createelement ("Add"); Xelem2.setattribute ("Key", AppKey); Xelem2.setattribute ("value", Appvalue); Xnode.appendchild (XELEM2); CfA. APPSETTINGS.SETTINGS.ADD (AppKey, Appvalue); } //Change the configuration file information in the cache (it will be read to be the latest configuration)CFA. Save (); Configurationmanager.refreshsection ("appSettings"); Xdoc.save (path); //Properties.Settings.Default.Reload (); } Catch(Exception e) {stringError =E.message; }
Can be downloaded directly using
SOURCE class: AppSettingHelper.cs
"C #" #103 dynamically modifying the app. Config profile