[C #] #103 dynamically modify the App. config configuration file,
PairC/S ModeUnder the App. config configuration fileAppSettingNode, supporting configuration informationChange to use nowAnd canPersistent Storage.
I.First, let's take a look at how to get the content in the configuration information.[This is recommended for obtaining configuration information]
1.1 obtain Method 1: reference the namespace before obtaining it:Using System. Configuration;
ConfigurationManager.AppSettings["key"]
1.2 Method 2: Load the configuration file directly using the XML class, and then read the information under the deleetting node.[Not recommended]
2. Write or modify the configuration I Information 【We recommend that you use both methods together.]
2.1 Method 1: Use the built-inConfigurationManagerClass
Problem: After writing,It cannot be stored persistently.And can only be used within the running range. When the program is closed, the stored information disappears.
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);cfa.AppSettings.Settings["SMTP"].Value = value;
2.2 Method 2: Use the XML class to load the configuration file, add the XMLNode node, or modify [directly modify the original file]
Problem: Modification/addition is complete,The latest configuration information cannot be used immediately.
Try {XmlDocument xDoc = new XmlDocument (); // get the App. absolute path of the config file String basePath = System. appDomain. currentDomain. setupInformation. applicationBase; String path = basePath. substring (0, basePath. length-10) + "App. config "; 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) {string error = e. message ;}
Recommended Methods:
Try {XmlDocument xDoc = new XmlDocument (); // get the App. absolute path of the config file String basePath = System. appDomain. currentDomain. setupInformation. applicationBase; basePath = basePath. substring (0, basePath. length-10); String path = basePath + "App. config "; xDoc. load (path); XmlNode xNode; XmlElement xElem1; XmlElement xElem2; // After modifying the file content, you also need to modify the configuration content in the cache, you can use the modified Configuration cfa only after modification. // if you do not modify the cache, you must wait until the program is closed and start. = 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);} // modify the configuration file information in the cache (read to obtain the latest configuration) cfa. save (); ConfigurationManager. refreshSection ("appSettings"); xDoc. save (path); // Properties. settings. default. reload ();} catch (Exception e) {string error = e. message ;}
You can download and use it directly.
Source code: etettinghelper. cs