WinForm Web 读写配置文件
来源:互联网 发布:美图秀秀做淘宝主图 编辑:程序博客网 时间:2024/06/10 07:51
.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", "key1s value");
appsection.settings.add("addkey2", "key2s value");
config.save();
运行代码之后可以看见配置文件中的改变:
<appsettings>
<add key="addkey1" value="key1s value" />
<add key="addkey2" value="key2s value" />
</appsettings>
修改和删除节点或属性也非常方便:
//打开配置文件
configuration config = webconfigurationmanager.openwebconfiguration("~");
//获取appsettings节点
appsettingssection appsection = (appsettingssection)config.getsection("appsettings");
//删除appsettings节点中的元素
appsection.settings.remove("addkey1");
//修改appsettings节点中的元素
appsection.settings["addkey2"].value = "modify key2s value";
config.save();
配置文件:
<appsettings>
<add key="addkey2" value="modify key2s value" />
</appsettings>
- WinForm Web 读写配置文件
- winform 读写配置文件
- WinForm 读写配置文件
- WinForm 创建与读写配置文件
- WinForm 创建与读写配置文件
- web项目读写配置文件
- c#,WinForm中读写配置文件App.config
- c# WinForm 创建与读写配置文件
- c# WinForm 创建与读写配置文件
- winform读写ini配置文件的类及相关调用
- c# winform app.config xml 配置文件 读写操作
- Web 下配置文件信息的读写
- Web 下配置文件信息的读写
- 读写配置文件(.congfig)步骤,web.config
- asp.net 读写配置文件Web.Config
- asp.net 读写配置文件Web.Config
- asp.net 读写配置文件Web.Config
- web和winform中读取config配置文件的方法
- wholesale nba jerseys 库特尼·李正式亮相休斯顿
- 转:究竟什么是POJO?
- 人品计算器
- __declspec(dllexport)的意思与DEF导出函数的区别
- OSPF七种状态及其排错
- WinForm Web 读写配置文件
- Ambarella面试小结
- Autoconf
- Dom4j下载及使用Dom4j读写XML简介
- 著名编程语录
- Java程序员和.NET程序员
- Dom4j下载及使用Dom4j读写XML简介
- Dom4j下载及使用Dom4j读写XML简介
- VC中 通过调用脚本进行SQL数据库的备份