But this variable will not have a fixed value and will change according to the actual situation. For example, when you need to read the path of a configuration file, and this path is the actual hard disk path published by the site, if it is directly the compile-time state, no problem. But if the site iis changes the path, you need to modify the parameters in this web.config. If this compile-time state can be modified into a run-time state, it will be more reasonable and convenient. This requires a solution that can dynamically modify web.config in code.
Code
/// <summary> /// 写入web.config /// </summary> /// <param name="item">appSettings等</param> /// <param name="key">键</param> /// <param name="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(); } }
For more specific methods of modifying web.config nodes in asp.net code, please pay attention to the PHP Chinese website for related articles!