C # ASP. NET Modify Webconfig file configuration

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.