ConfigurationManager类在System.Configuration中,WebConfigurationManager在System.Web.Configuration中。
根据MSDN的解释,对于 Web 应用程序配置,建议使用 System.Web.Configuration.WebConfigurationManager 类,而不要使用 System.Configuration.ConfigurationManager 类。
//打开配置文件
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
//获取appSettings节点
AppSettingsSection appSection = (AppSettingsSection)config.GetSection("appSettings");
//在appSettings节点中添加元素
appSection.Settings.Add("addkey1", "key1's value");
appSection.Settings.Add("addkey2", "key2's value");
config.Save(); 运行代码之后可以看见配置文件中的改变:
<appSettings>
<add key="addkey1" value="key1's value" />
<add key="addkey2" value="key2's value" />
</appSettings>
//打开配置文件
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
//获取appSettings节点
AppSettingsSection appSection = (AppSettingsSection)config.GetSection("appSettings");
//删除appSettings节点中的元素
appSection.Settings.Remove("addkey1");
//修改appSettings节点中的元素
appSection.Settings["addkey2"].Value = "Modify key2's value";
config.Save();
<appSettings>
<add key="addkey2" value="Modify key2's value" />
</appSettings>
本文详细介绍了在ASP.NET 2.0中如何使用ConfigurationManager和WebConfigurationManager类来高效地管理配置文件,包括读取、添加、修改和删除配置项的具体实现。

744

被折叠的 条评论
为什么被折叠?



