Refresh after updating the application's configuration file
Configurationmanager.refreshsection ("appSettings");/refresh the named section and re-read it from disk the next time it is retrieved.
ConfigurationSettings also exists this problem, but I still do not know how to refresh the node, hehe.
Old method: You reader better use the "new method" below
Configuration file:
| The code is as follows |
Copy Code |
<configuration> <appSettings> <add key= "name" value= "I am a remote server"/> </appSettings> </configuration> |
Background programs are worth reading:
| The code is as follows |
Copy Code |
String s=system.configuration.configurationsettings.appsettings["name"]; |
To modify the value of a configuration file:
| code is as follows |
copy code |
| ///< Summary> ///Update profile information ///</summary> ///<param name= "name" > Profile field name </param ///<param name= "Xvalue" > Value </param> private void Updateconfig (String name,string xvalue) { XmlDocument doc = new XmlDocument (); Doc. Load (Application.executablepath + ". config"); XmlNode node = doc. selectSingleNode (@ "//add[@key = '" +name+ "]"); XmlElement ele = (xmlelement) node; Ele. SetAttribute ("value", xvalue); Doc. Save (Application.executablepath + ". config"); } |
To insert a value into a configuration file:
| The code is as follows |
Copy Code |
| <summary> Write information to the Appkey knot of the. config file Appvalue Save settings </summary> <param name= "Appkey" > Festival naming </param> <param name= "Appvalue" > Value </param> Private void SetValue (String appkey,string appvalue) { XmlDocument xdoc=new XmlDocument (); Xdoc.load (system.windows.forms.application.executablepath+ ". config"); 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 (system.windows.forms.application.executablepath+ ". config"); } |
New method:
| The code is as follows |
Copy Code |
system.configuration.configurationsettings.appsettings["Key"];
|
But now FrameWork2.0 has made it clear that this attribute is obsolete. It was suggested that the change be ConfigurationManager or WebConfigurationManager. And the AppSettings property is read-only and does not support modifying property values.
However, to invoke ConfigurationManager you must first add a System.Configuration.dll assembly reference to the project. (Right-click the project name in Solution Manager and select Add Reference in the right menu to be found under. NET Tablepage) you can use String str = configurationmanager.appsettings["Key" after adding a reference To get the corresponding value.
Update configuration file:
| The code is as follows |
Copy Code |
Configuration CFA = configurationmanager.openexeconfiguration (Configurationuserlevel.none); Add to CfA. APPSETTINGS.SETTINGS.ADD ("Key", "Name") Modify CfA. appsettings.settings["Browsedir"]. Value = "name"; |
Last Call
| The code is as follows |
Copy Code |
CfA. Save ();
|
The current profile update was successful.
| The code is as follows |
Copy Code |
| Configurationmanager.refreshsection ("appSettings"); |
Refreshes the named section and will re-read it from disk the next time it is retrieved. Remember that the application will refresh the node