C#中读写自定义的web 配置文件
开发程序的过程中,有时候我们需要自己编写一个config文件,比如取名App.config, 然后写一些配置信息在里面。然后我们需要编写C#代码来对这个配置文件进行读写
比如:App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="mySection" type="test.MySection, test"/>
</configSections>
<mySection>
<add key="user1" value="aaa111" />
<add key="user2" value="bbb111" />
</mySection>
</configuration>
可以看出在这个配置文件中,我们自己定义了一个section => mySection,这里自定义的section都需要从ConfigurationSection中继承, 所以我们写一个MySection类,从ConfigurationSection中继承
using System;
using System.Configuration; public class MySection : ConfigurationSection // 所有配置节点都要选择这个基类
{
private static ConfigurationProperty s_property = new ConfigurationProperty(string.Empty, typeof(MyKeyValueCollection), null, ConfigurationPropertyOptions.IsDefaultCollection); [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
public MyKeyValueCollection KeyValues {
get {
return (MyKeyValueCollection)base[s_property];
}
}
[ConfigurationCollection(typeof(MyKeyValueSetting))]
public class MyKeyValueCollection : ConfigurationElementCollection // 自定义一个集合
{
// 基本上,所有的方法都只要简单地调用基类的实现就可以了。
public MyKeyValueCollection()
: base(StringComparer.OrdinalIgnoreCase) // 忽略大小写
{ } // 其实关键就是这个索引器。但它也是调用基类的实现,只是做下类型转就行了。
new public MyKeyValueSetting this[string name] {
get { return (MyKeyValueSetting)base.BaseGet(name); }
set { base[name] = value; }
} // 下面二个方法中抽象类中必须要实现的。
protected override ConfigurationElement CreateNewElement() {
return new MyKeyValueSetting();
} protected override object GetElementKey(ConfigurationElement element) {
return ((MyKeyValueSetting)element).Key;
} // 说明:如果不需要在代码中修改集合,可以不实现Add, Clear, Remove
public void Add(MyKeyValueSetting setting) {
this.BaseAdd(setting);
} public void Clear() {
base.BaseClear();
} public void Remove(string name) {
base.BaseRemove(name);
}
}
public class MyKeyValueSetting : ConfigurationElement // 集合中的每个元素
{ [ConfigurationProperty("key", IsRequired = true)]
public string Key {
get { return this["key"].ToString(); }
set { this["key"] = value; }
} [ConfigurationProperty("value", IsRequired = true)]
public string Value {
get { return this["value"].ToString(); }
set { this["value"] = value; }
}
} }
读取App.config file中的section
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var mySection = config.GetSection("mySection") as MySection;
foreach (MySection.MyKeyValueSetting add in mySection.KeyValues) {
Console.WriteLine(string.Format("{0}-{1}", add.Key, add.Value));
}
往App.config中写section
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var mySection = config.GetSection("mySection") as MySection; mySection.KeyValues.Clear();
mySection.KeyValues.Add(new MySection.MyKeyValueSetting() { Key = "aaaaaa", Value = "ddddddd" }); config.Save();
ConfigurationManager.RefreshSection("mySection"); //刷新
C#中读写自定义的web 配置文件的更多相关文章
- 在.net中读写config文件的各种方法(自定义config节点)
http://www.cnblogs.com/fish-li/archive/2011/12/18/2292037.html 阅读目录 开始 config文件 - 自定义配置节点 config文件 - ...
- [转]通过继承ConfigurationSection,在web.config中增加自定义配置
本文转自:http://www.blue1000.com/bkhtml/2008-02/55810.htm 前几天写了一篇使用IConfigurationSectionHandler在web.conf ...
- 在配置文件(.settings、.config)中存储自定义对象
原文:在配置文件(.settings..config)中存储自定义对象 引言 我前面曾写过一篇<使用配置文件(.settings..config)存储应用程序配置>,我在其中指出“sett ...
- C#读写自定义的多字段配置文件
mark一下,日后填坑 参考: WPF 读写自己写的配置文件
- 在.net中读写config文件的各种方法
阅读目录 开始 config文件 - 自定义配置节点 config文件 - Property config文件 - Element config文件 - CDATA config文件 - Collec ...
- 在.net中读写config文件的各种方法【转】
今天谈谈在.net中读写config文件的各种方法. 在这篇博客中,我将介绍各种配置文件的读写操作. 由于内容较为直观,因此没有过多的空道理,只有实实在在的演示代码, 目的只为了再现实战开发中的各种场 ...
- 重新想象 Windows 8.1 Store Apps (90) - 通信的新特性: 通过 HttpBaseProtocolFilter 实现 http 请求的缓存控制,以及 cookie 读写; 自定义 HttpFilter; 其他
[源码下载] 重新想象 Windows 8.1 Store Apps (90) - 通信的新特性: 通过 HttpBaseProtocolFilter 实现 http 请求的缓存控制,以及 cooki ...
- 自定义WCF的配置文件
原文地址:http://www.cnblogs.com/shanyou/archive/2008/12/02/1346298.html WCF的承载既可以通过编码实现,也能够通过配置实现.而且使用配置 ...
- Spring Boot中的自定义start pom
start pom是springboot中提供的简化企业级开发绝大多数场景的一个工具,利用好strat pom就可以消除相关技术的配置得到自动配置好的Bean. 举个例子,在一般使用中,我们使用基本的 ...
随机推荐
- 大话设计模式--享元模式 Flyweight -- C++实现实例
1. 享元模式: 运用共享技术有效地支持大量细粒度的对象. 享元模式可以避免大量非常相似类的开销,在程序设计中,有时需要生成大量颗粒度的类实例来表示数据,如果能发现这些实例除了几个参数外基本都是相同的 ...
- JAVA- 切换默认的Java
删除自带的java yum remove java java -version发现还有java,因为电脑上安装了多个版本的java,这时我们可以用 yum groupremove java 通过组的这 ...
- PHP中有多态么
PHP中有多态么 一.总结 一句话总结:封装是类的构建过程,php具有:php也具有继承的特性.唯独这个多态,php体现的十分模糊.原因是php是弱类型语言. php不具有像java那种清晰的多态,不 ...
- JQuery 常用代码
1.选择器 1.根据标签名: $('p') 选择文档中的所有段落 2. 根据ID: $("#some-id") 3.类: $('.some-class') $('.t ...
- Ueditor基础使用
感谢大家对我这个菜鸟的帮助,这是我第一次用.NET做网站.在这里向大家推荐个百度免费的文本编辑器Ueditor,是.NET版的,在http://ueditor.baidu.com/website/in ...
- 深度学习—BN的理解(二)
神经网络各个操作层的顺序: 1.sigmoid,tanh函数:conv -> bn -> sigmoid -> pooling 2.RELU激活函数:conv -> bn -& ...
- Hibernate学习---第十四节:hibernate之session线程安全
1.hibernate.cfg.xml 文件中添加如下代码开启线程安全: <property name="hibernate.current_session_context_class ...
- 几个常用的url生成二维码的接口
找到了几个URL生成的接口,速度上可能会有差别,可试验后选用,我用过第一个,分享: <!doctype html> <html lang="en"> < ...
- mysql 常用的存储引擎MyISAM/InnoDB比较
- 关于avpicture_fill 和 sws_scale的关系
avpicture_fill((AVPicture *) pFrameRGB, buffer, PIX_FMT_RGB565, pCodecCtx->width, pCodecCtx->h ...