Configuration files are used in many cases. configuration files are divided into application configuration files and web configuration files.
The biggest difference between the two configuration files is that the web configuration files are updated in real time and the application configuration files are not updated in real time.
Update the application configuration file
ConfigurationManager. RefreshSection ("appSettings"); // refresh the naming section and read it from the disk the next time you retrieve it.
This problem also exists in ConfigurationSettings, but I still don't know how to refresh the node.
Old method: You are advised to use the following "New Method"
Configuration file:
<Configuration>
<Deleetask>
</AppSettings>
</Configuration>
The background program is worth reading:
String s = System. Configuration. ConfigurationSettings. receivettings ["name"];
Modify the configuration file value:
?
1234567891011121314 |
/// <summary> /// Update the configuration file information /// </summary> /// <Param name = "name"> name of the configuration file field </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" ); } |
Insert values to the configuration file:
?
12345678910111213141516171819202122232425 |
///<summary> /// Write information to the appKey of the. config file AppValue Save settings ///</summary> /// <Param name = "AppKey"> node name </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:
System. Configuration. ConfigurationSettings. receivettings ["Key"];
However, FrameWork2.0 clearly indicates that this attribute is out of date. We recommend that you change it to ConfigurationManager or WebConfigurationManager. The AppSettings attribute is read-only and does not support modifying attribute values.
However, to call ConfigurationManager, you must add an Assembly reference in the project. (In solution manager, right-click the project name and choose add reference from the context menu ,. net TablePage.) After adding a reference, you can use String str = ConfigurationManager. appSettings ["Key"] to obtain the corresponding value.
Update the configuration file:
Configuration cfa = ConfigurationManager. OpenExeConfiguration (ConfigurationUserLevel. None );
// Add
Cfa. shortettings. Settings. Add ("key", "Name ")
// Modify
Cfa. deleettings. Settings ["BrowseDir"]. Value = "name ";
Last call
Cfa. Save ();
The current configuration file is updated successfully.