开发程序的过程中,有时候我们需要自己编写一个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 配置文件的更多相关文章

  1. 在.net中读写config文件的各种方法(自定义config节点)

    http://www.cnblogs.com/fish-li/archive/2011/12/18/2292037.html 阅读目录 开始 config文件 - 自定义配置节点 config文件 - ...

  2. [转]通过继承ConfigurationSection,在web.config中增加自定义配置

    本文转自:http://www.blue1000.com/bkhtml/2008-02/55810.htm 前几天写了一篇使用IConfigurationSectionHandler在web.conf ...

  3. 在配置文件(.settings、.config)中存储自定义对象

    原文:在配置文件(.settings..config)中存储自定义对象 引言 我前面曾写过一篇<使用配置文件(.settings..config)存储应用程序配置>,我在其中指出“sett ...

  4. C#读写自定义的多字段配置文件

    mark一下,日后填坑 参考: WPF 读写自己写的配置文件

  5. 在.net中读写config文件的各种方法

    阅读目录 开始 config文件 - 自定义配置节点 config文件 - Property config文件 - Element config文件 - CDATA config文件 - Collec ...

  6. 在.net中读写config文件的各种方法【转】

    今天谈谈在.net中读写config文件的各种方法. 在这篇博客中,我将介绍各种配置文件的读写操作. 由于内容较为直观,因此没有过多的空道理,只有实实在在的演示代码, 目的只为了再现实战开发中的各种场 ...

  7. 重新想象 Windows 8.1 Store Apps (90) - 通信的新特性: 通过 HttpBaseProtocolFilter 实现 http 请求的缓存控制,以及 cookie 读写; 自定义 HttpFilter; 其他

    [源码下载] 重新想象 Windows 8.1 Store Apps (90) - 通信的新特性: 通过 HttpBaseProtocolFilter 实现 http 请求的缓存控制,以及 cooki ...

  8. 自定义WCF的配置文件

    原文地址:http://www.cnblogs.com/shanyou/archive/2008/12/02/1346298.html WCF的承载既可以通过编码实现,也能够通过配置实现.而且使用配置 ...

  9. Spring Boot中的自定义start pom

    start pom是springboot中提供的简化企业级开发绝大多数场景的一个工具,利用好strat pom就可以消除相关技术的配置得到自动配置好的Bean. 举个例子,在一般使用中,我们使用基本的 ...

随机推荐

  1. vim设置tab为4空格

    vim的最基础设置 vim的设置需要编辑~/.vimrc文件,更改已有设置或者在后面添加相应的设置. 设置tab为4字符 # ts: tabstop set ts=4 将tab展开为空格 # expa ...

  2. 单链表(C++实现)

    单链表的结构有多种 这里介绍的链表有头结点.有尾节点并且尾节点指向头结点 单链表的每个结点的地址存放在其直接前驱结点的指针域中.其中第一个结点没有前驱结点,因此需要一个头指针指向第一个节点,便于我们对 ...

  3. django配置文件环境分离后celery的启动方式整理

    django项目中,当配置文件分离时: 启动方式1: 硬编码写死在manage.py中: os.environ.setdefault("DJANGO_SETTINGS_MODULE" ...

  4. Spring Cloud之统一fallback接口

    每个方法都配备一个fallback方法 不利于开发的 用类的方式 并且整个方法都是在同一个线程池里面的 主要对于client的修改: pom: <project xmlns="http ...

  5. Spark集群搭建(local、standalone、yarn)

    Spark集群搭建 local本地模式 下载安装包解压即可使用,测试(2.2版本)./bin/spark-submit --class org.apache.spark.examples.SparkP ...

  6. matlab画折线

    figure(721);hold on;x=1:1:5;%x轴上的数据,第一个值代表数据开始,第二个值代表间隔,第三个值代表终止 a=[203.024,113.857,256.259,244.888, ...

  7. 机器学习(二十三)— L0、L1、L2正则化区别

    1.概念  L0正则化的值是模型参数中非零参数的个数. L1正则化表示各个参数绝对值之和. L2正则化标识各个参数的平方的和的开方值. 2.问题  1)实现参数的稀疏有什么好处吗? 一个好处是可以简化 ...

  8. appium-环境搭建(一)

    adb命令 adb的全称为Android Debug Bridge,就是起到调试桥的作用.借助adb工具,我们可以管理设备或者手机模拟器的状态.还可以进行很多手机操作,如安装软件\系统升级\运行she ...

  9. hibernate复习第(4)天

    1.hibernate的映射类型.hbm.xml中property中的type属性.这个type属性是表示持久化类中的属性对应数据库中的什么数据类型,用来构建一种映射type的可选值:hibernat ...

  10. 关于MFC主菜单和右键弹出菜单

    一.主菜单.弹出菜单和右键菜单的概念: 主菜单是窗口顶部的菜单,一个窗口或对话框只能有一个主菜单,但是主菜单可以被更改(SetMenu()更改): 创建方式:CMenu::CreateMenu(voi ...