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