A function used to modify the appSettings configuration section in web. config.

Source: Internet
Author: User

This function mainly uses XmlDocument to parse web. config. and uses the SelectSingleNode () method to locate the configuration section to be modified. Note that the last program must be saved (). Therefore, your apsnet account must have write permission on web. config.
--------------------------------------------------------------------------------
/// <Summary>
/// Modify the value Attribute in Add in the configuration section of the web. config file appSettings.
/// </Summary>
/// <Remarks>
/// Note: after this function is called, the entire Web Application will be restarted, resulting in the loss of all sessions currently
/// </Remarks>
/// <Param name = "key"> key to be modified </param>
/// <Param name = "strValue"> modified value </param>
/// <Exception cref = ""> related keys cannot be found </exception>
/// <Exception cref = ""> the permission is insufficient and cannot be saved to the web. config file. </exception>
Public void Modify (string key, string strValue)
{
String XPath = "/configuration/appSettings/add [@ key = '? '] ";
XmlDocument domWebConfig = new XmlDocument ();

DomWebConfig. Load (HttpContext. Current. Server. MapPath ("web. config ")));
XmlNode addKey = domWebConfig. SelectSingleNode (XPath. Replace ("? ", Key )));
If (addKey = null)
{
Throw new ArgumentException ("configuration section of <add key = '" + key + "'value =.../> not found ");
}
AddKey. Attributes ["value"]. InnerText = strValue;
DomWebConfig. Save (HttpContext. Current. Server. MapPath ("web. config ")));

}

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.