#regionModify config file/// <summary> ///Modify config file (appsetting node)/// </summary> /// <param name= "key" >Key</param> /// <param name= "value" >the value to modify into</param> Public Static voidUpdateappsetting (stringKeystringvalue) {XmlDocument doc=NewXmlDocument (); //get the full path of the configuration file stringstrFileName = AppDomain.CurrentDomain.BaseDirectory.ToString () +"Web. config"; Doc. Load (strFileName); //find all elements with the name "add"XmlNodeList nodes = doc. getElementsByTagName ("Add"); for(inti =0; I < nodes. Count; i++) { //gets the key property of the current elementXmlAttribute _key = nodes[i]. attributes["Key"]; //determines whether the current element is a target element based on the first attribute of the element if(_key! =NULL) { if(_key. Value = =key) { //assign a value to the second attribute in the target element_key = Nodes[i]. attributes["value"]; _key. Value=value; Break; } } } //Save the changes aboveDoc. Save (strFileName); } /// <summary> ///Modify config file (connectionstring node)/// </summary> /// <param name= "name" >Key</param> /// <param name= "value" >the value to modify into</param> Public Static voidUpdateconnectionstring (stringNamestringvalue) {XmlDocument doc=NewXmlDocument (); //get the full path of the configuration file stringstrFileName = AppDomain.CurrentDomain.BaseDirectory.ToString () +"Web. config"; Doc. Load (strFileName); //find all elements with the name "add"XmlNodeList nodes = doc. getElementsByTagName ("Add"); for(inti =0; I < nodes. Count; i++) { //gets the key property of the current elementXmlAttribute _name = nodes[i]. attributes["name"]; //determines whether the current element is a target element based on the first attribute of the element if(_name! =NULL) { if(_name. Value = =name) { //assign a value to the second attribute in the target element_name = Nodes[i]. attributes["connectionString"]; _name. Value=value; Break; } } } //Save the changes aboveDoc. Save (strFileName); } #endregion
C # ASP. NET Modify Webconfig file configuration