Modify and obtain the value attribute function in Add in the configuration section of the appSettings of the web. config or app. config file.

Source: Internet
Author: User

   1:  /// <summary>
2: // modify the value Attribute in Add in the appSettings configuration section of the web. config or app. config file.
   3:  /// </summary>
   4:  /// <remarks>
5: // Note: after calling this function, the entire Web Application will be restarted, resulting in the loss of all sessions currently.
   6:  /// </remarks>
7: // <param name = "key"> key to be modified </param>
8: // <param name = "strValue"> modified value </param>
9: // <exception cref = ""> the related key cannot be found. </exception>
10: // <exception cref = ""> the permission is insufficient and cannot be saved to the web. config file. </exception>
  11:  public static void ModifyAppSettings(string key, string strValue)
  12:  {
  13:      string XPath = "/configuration/appSettings/add[@key='?']";
  14:      XmlDocument domConfig = new XmlDocument();
  15:   
  16:      domConfig.Load(Environment.CurrentDirectory + "/Application1.exe.config");
  17:      XmlNode addKey = domConfig.SelectSingleNode((XPath.Replace("?", key)));
  18:      if (addKey == null)
  19:      {
20: throw new ArgumentException ("configuration section of <add key = '" + key + "'value =.../> not found ");
  21:      }
  22:      addKey.Attributes["value"].InnerText = strValue;
  23:      domConfig.Save(Environment.CurrentDirectory + "/Application1.exe.config");
  24:   
  25:  }
  26:   
  27:  /// <summary>
28: // obtain the value attribute in Add in the appSettings configuration section of the web. config or app. config file.
  29:  /// </summary>
30: /// <param name = "key"> key to be modified </param>
31: // <param name = "strValue"> modified value </param>
32: // <exception cref = ""> the related key cannot be found. </exception>
33: // <exception cref = ""> the permission is insufficient and cannot be saved to the web. config file. </exception>
  34:  public static string GetAppSettings(string key)
  35:  {
  36:      string XPath = "/configuration/appSettings/add[@key='?']";
  37:      XmlDocument domConfig = new XmlDocument();
  38:   
  39:      domConfig.Load(Environment.CurrentDirectory + "/Application1.exe.config");
  40:      XmlNode addKey = domConfig.SelectSingleNode((XPath.Replace("?", key)));
  41:      if (addKey == null)
  42:      {
43: throw new ArgumentException ("configuration section of <add key = '" + key + "'value =.../> not found ");
  44:      }
  45:      return addKey.Attributes["value"].InnerText;
  46:  }

Related Article

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.