Modify the web. config node in asp.net code

Source: Internet
Author: User

In some cases, it is common to read a global variable in the Code and put it in web. config. However, this variable does not have a fixed value and changes according to the actual situation. For example, you need to read the path of a configuration file, which is the actual hard disk path released by the site, if the status is compile-time, no problem occurs. However, if you change the site iis path, you need to modify the parameters in web. config. It is more reasonable and convenient to change the status during compilation to the running status. This requires a scheme to dynamically modify web. config in the code.
View code
 
1 /// <summary>
2 // write to web. config
3 /// </summary>
4 /// <param name = "item"> receivettings and so on </param>
5 // <param name = "key"> key </param>
6 /// <param name = "value"> value </param>
7 public void WriteConfig (string item, string key, string value)
8 {
9 if (item = "")
10 {
11 item = "maid ";
12}
13 Configuration config = System. Web. Configuration. WebConfigurationManager. OpenWebConfiguration (System. Web. HttpContext. Current. Request. ApplicationPath );
14 AppSettingsSection appSection = (AppSettingsSection) config. GetSection (item );
15 if (appSection. Settings [key] = null)
16 {
17 appSection. Settings. Add (key, value );
18 config. Save ();
19}
20 else
21 {
22 appSection. Settings. Remove (key );
23 appSection. Settings. Add (key, value );
24 config. Save ();
25}
26}
 
 
 


Author: xTechnet

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.