Specific methods for modifying web.config nodes in asp.net code _ Practical Tips

Source: Internet
Author: User
But this variable will not be a fixed value, will change according to the actual situation, such as in the need to read a configuration file path, and this path is the site published the actual hard drive path, if directly is the compile-time state, no problem. However, if site IIS replaces the path, you need to modify the parameters in this web.config. It would be more reasonable and convenient if you could modify this compile-time state to a run-time state. This requires a scenario in which Web.config can be dynamically modified in code.
Code
Copy Code code as follows:

<summary>
Write to Web.config
</summary>
<param name= "Item" >appsettings etc </param>
<param name= "key" > Key </param>
<param name= "Value" > Value </param>
public void Writeconfig (string item, string key, String value)
{
if (item = = "")
{
item = "AppSettings";
}
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration ( System.Web.HttpContext.Current.Request.ApplicationPath);
Appsettingssection appsection = (appsettingssection) config. GetSection (item);
if (appsection.settings[key] = = null)
{
APPSECTION.SETTINGS.ADD (key, value);
Config. Save ();
}
Else
{
AppSection.Settings.Remove (key);
APPSECTION.SETTINGS.ADD (key, value);
Config. Save ();
}
}

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.