WebConfigurationManager 使用技巧
来源:互联网 发布:手机淘宝5.7 编辑:程序博客网 时间:2024/06/11 21:28
net1.1中如果需要灵活的操作和读写配置文件并不是十分方便,一般都会在项目中封装一个配置文件管理类来进行读写操作。而在.net2.0中使用ConfigurationManager 和WebConfigurationManager 类可以很好的管理配置文件,ConfigurationManager类在System.Configuration 中,WebConfigurationManager在System.Web.Configuration中。根据MSDN的解释,对于 Web 应用程序配置,建议使用 System.Web.Configuration.WebConfigurationManager 类,而不要使用 System.Configuration.ConfigurationManager 类。
如何使用WebConfigurationManager操作配置文件:
//打开配置文件
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();
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>
<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();
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>
<add key="addkey2" value="Modify key2's value" />
</appSettings>
以上转载自:http://hi.baidu.com/%C5%D4%B0%D7%D0%D6/blog/item/c79af6940c7c93017bf480cb.html
- WebConfigurationManager 使用技巧
- C#使用WebConfigurationManager类修改Web.config文件
- C#使用WebConfigurationManager类修改Web.config文件
- WebConfigurationManager相关
- WebConfigurationManager 类
- WebConfigurationManager读写配置文件
- WebConfigurationManager.OpenWebConfiguration("~")未能映射路径
- 【技巧】UE使用技巧
- 使用技巧
- 读取web.config配置的类WebConfigurationManager与ConfigurationManager对比
- asp.net 用WebConfigurationManager读取和修改配置文件
- eclipse使用技巧:技巧汇总
- 【Xcode使用技巧】Xcode5.1.1 使用技巧
- 【MAC使用技巧】QuickTime Player使用技巧
- JavaBean使用技巧
- TextBox使用技巧
- Linux 使用中的技巧
- VisualAge使用技巧
- 关闭任务管理器杀不了的进程
- ant
- 开源技术选型手册 (china-pub 首发) -目 录
- 数据库设计十四技巧
- Javascript中最常用的55个经典技巧
- WebConfigurationManager 使用技巧
- 升级复制数据库 2000到2005
- 25个PHP游戏编程脚本代码
- 在UpdatePanel中使用验证控件 (asp.net C#)
- Qt应用程序的图标
- Asterisk Summary - 2
- 12种健康杀手,全是咱们常见的食品啊!
- 在servlet中如何获取application实例
- Android G1刷机攻略