1. 定义自己的KeyValue

<section name="TestKeyValue" type="System.Configuration.NameValueSectionHandler"></section>
<TestKeyValue>
<add key="aaa" value="aaa"/>
<add key="bbb" value="bbbb"/>
<add key="ccc" value="ccccc"/>
</TestKeyValue>
var testKeyValue = ConfigurationManager.GetSection("TestKeyValue") as System.Collections.Specialized.NameValueCollection;

2. 完全自定义section(类型自定义)

<section name="TEST" type="TestApplication.Test, TestApplication"></section>
<TEST AAA="10">
<BBB CCC="20"></BBB>
<DDD>
<add key="aa" value="aaa"></add>
<add key="bb" value="bbb"></add>
</DDD>
</TEST>
public class Test : ConfigurationSection/
{
[ConfigurationProperty("AAA", IsRequired = true)]
public int AAA
{
get
{
return (int)base["AAA"];
}
set
{
base["AAA"] = value;
}
} [ConfigurationProperty("BBB", IsRequired = false)]
public BBB BBB
{
get
{
return (BBB)base["BBB"];
}
set
{
base["BBB"] = value;
}
} [ConfigurationProperty("DDD", Options = ConfigurationPropertyOptions.IsDefaultCollection, IsRequired = true)]
public NameValueFileCollection DDD
{
get
{
return (NameValueFileCollection)base["DDD"];
}
}
} public class BBB : ConfigurationElement
{
[ConfigurationProperty("CCC", IsRequired = true)]
public int CCC
{
get { return (int)base["CCC"]; }
set { base["CCC"] = value; }
}
} [ConfigurationCollection(typeof(KeyValueConfigurationElement))]
public class NameValueFileCollection : ConfigurationElementCollection
{
new public KeyValueConfigurationElement this[string name]
{
get
{
return (KeyValueConfigurationElement)base.BaseGet(name);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new KeyValueConfigurationElement();
} protected override object GetElementKey(ConfigurationElement element)
{
return ((KeyValueConfigurationElement)element).Key;
}
} public class KeyValueConfigurationElement : ConfigurationElement
{
[ConfigurationProperty("key", IsRequired = true)]
public string Key
{
get { return base["key"].ToString(); }
set { base["key"] = value; }
} [ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get { return base["value"].ToString(); }
set { base["value"] = value; }
}
}
var read = ConfigurationManager.GetSection("TEST") as Test;

3. 定义SectionGroup,SectionGroup不是Collection,里面是多个Section,每个Section按照上面的方式定义,获取取Configuration已有的定义。

<sectionGroup name="SectionGroup" >
<section name="TestGroup" type="TestApplication.TestGroup, TestApplication"/>
<section name="TestGroup2" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup> <SectionGroup>
<TestGroup>
<add Name="zhangsan" Age="19" Gender="true" />
<add Name="lisi" Age="20" Gender="false" />
<add Name="wangwu" Age="22" Gender="true" />
</TestGroup>
<TestGroup2>
<add key="A" value="aaa"/>
<add key="B" value="bbb"/>
</TestGroup2>
</SectionGroup>
public class TestGroup : ConfigurationSection
{
public static TestGroup FromConfigFile()
{
return (TestGroup)ConfigurationManager.GetSection("SectionGroup");
} [ConfigurationProperty("", DefaultValue = null, IsDefaultCollection = true, IsRequired = false)]
public XDCollection Content
{
get { return (XDCollection)base[new ConfigurationProperty("", typeof(XDCollection), null, ConfigurationPropertyOptions.IsDefaultCollection)]; }
}
} [ConfigurationCollection(typeof(TestGroupElement))]
public class XDCollection : ConfigurationElementCollection
{
new public TestGroupElement this[string name]
{
get { return (TestGroupElement)base[name]; }
} protected override ConfigurationElement CreateNewElement()
{
return new TestGroupElement();
} protected override object GetElementKey(ConfigurationElement element)
{
return ((TestGroupElement)element).Name;
}
} public class TestGroupElement : ConfigurationElement
{
[ConfigurationProperty("Name", DefaultValue = "JLQ", IsRequired = false)]
public string Name { get { return (string)base["Name"]; } set { base["Name"] = value; } } [ConfigurationProperty("Age", DefaultValue = , IsRequired = false)]
public int Age { get { return (int)base["Age"]; } set { base["Age"] = value; } } [ConfigurationProperty("Gender", DefaultValue = false, IsRequired = false)]
public bool Gender { get { return (bool)base["Gender"]; } set { base["Gender"] = value; } }
}
var testGroup = ConfigurationManager.GetSection("SectionGroup/TestGroup") as TestGroup;
var testGroup2 = ConfigurationManager.GetSection("SectionGroup/TestGroup2") as System.Collections.Specialized.NameValueCollection;

4. 继承IConfigurationSectionHandler

<sectionGroup name="companyInfo">
<section name="companyAddress" type="TestApplication.Test3,TestApplication"/>
</sectionGroup>
<companyInfo>
<companyAddress>
<companyName>Axxonet Solutions India Pvt Ltd</companyName>
<doorNo>1301</doorNo>
<street>13th Cross, Indira Nagar, 2nd Stage</street>
<city>Bangalore</city>
<postalCode>560038</postalCode>
<country>India</country>
</companyAddress>
</companyInfo>
public class Test3 : IConfigurationSectionHandler
{
public string CompanyName { get; set; }
public string DoorNo { get; set; }
public string Street { get; set; }
public string City { get; set; }
public int PostalCode { get; set; }
public string Country { get; set; } public object Create(object parent, object configContext, XmlNode section)
{
Test3 one = new Test3();
one.CompanyName = section.SelectSingleNode("companyName").InnerText;
one.DoorNo = section.SelectSingleNode("doorNo").InnerText;
one.Street = section.SelectSingleNode("street").InnerText;
one.City = section.SelectSingleNode("city").InnerText;
one.PostalCode =
Convert.ToInt32(section.SelectSingleNode("postalCode").InnerText);
one.Country = section.SelectSingleNode("country").InnerText;
return one;
}
}
Test3 test3 = (Test3)ConfigurationManager.GetSection("companyInfo/companyAddress");

参考:http://www.codeproject.com/Articles/10981/Understanding-Section-Handlers-App-config-File

config中自定义配置的更多相关文章

  1. 项目文件中含有两个config文件,app.config与app1.config,如何获取app1.config中的配置

    想要通过配置文件配置C#前台画面,好奇做了以下测试:在项目中新建了app.config与app1.config两个配置文件,请教一下各位高手如果想从app1.config中读取配置信息应该如何读取?采 ...

  2. [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法

    本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...

  3. web.config or app.config 中configSections配置节点

    以前还真没见过,今天看项目中有在用,简单写了个Demo,这样配置的好处就是可以自定义配置,更加模块化,直接上代码; 1.配置文件 由于我创建的是一个控制台项目,所以配置文件是App.Config:(这 ...

  4. C# 中自定义配置

    微软在ConfigurationManager类里面为我们提供了AppSetting和ConnectionStrings 两个常用配置, 但是有时候我们需要自定的配置,例如 <image lef ...

  5. c# 操作.config中AppSettings配置节

    ConfigurationSettings.AppSettings[key].ToString(); 这种方式很眼熟吧? 不过这种方式基本过时了,虽然还能用. 微软建议采用ConfigurationM ...

  6. ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节

    主要代码,一定要继续System.Configuration.ConfigurationSection,具体的节点名称可以自行修改 using System; using System.Data; u ...

  7. EntityFramework在root目录web.config中的配置设置

    未找到具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序的实体框架提供程序.请确保在应用程序配置文件的“entityFramework”节中注册了该提供程序.有关详 ...

  8. springboot读取application.properties中自定义配置

    假设在application-xxx.properties中配置 user.name=yuhk 一.在Controller中读取 @Value("{$user.name}") pr ...

  9. ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节集合

    核心代码 using System; using System.Data; using System.Configuration; using System.Web; using System.Web ...

随机推荐

  1. 委托、匿名委托、Lambda 表达式、Expression表达式树之刨根问底

    本篇不是对标题所述之概念的入门文章,重点在阐述它们的异同点和应用场景.各位看官,这里就不啰嗦了,直接上代码. 首先定义一个泛型委托类型,如下: public delegate T Function&l ...

  2. WCF的一点补充-Restful相关

    参考 配置WCF心得 对REST架构 风格下WCF的一点补充 Securing WCF REST Service with Azure AppFabric Access Control Service ...

  3. 让Sqlite脱离VC++ Runtime独立运行

    前段时间在开发OrayTalk(傲瑞通)的聊天记录模块时用到了Sqlite,这是我第一次接触和使用Sqlite,总体感觉还是非常不错的.这里把我使用Sqlite的经验跟大家分享一下. 一.关于Sqli ...

  4. kali linux系列之启用vpn

    kali linux系列之启用vpn 文/玄魂 默认情况下,kali linux的vpn选项是不可用的. 下面是安装openvpn的方法,同样的,可以安装其他类型的vpn. 打开终端输入命令: Apt ...

  5. 作业七:团队项目——Alpha版本冲刺阶段-02

    昨天进展:框架设计以及菜单设计. 今天安排:完善界面设计以及象棋图片的绘制. 小组一共三人,陈芝航因家里有事,与我们进行了QQ视屏会议.

  6. django上传文件

    template html(模板文件): <form enctype="multipart/form-data" method="POST" action ...

  7. centos基本操作

    yum install nodejs npm install -g shadowsocks nohup ssserver & 后台运行 vi /usr/lib/node_modules/sha ...

  8. Jstat在分析java的内存GC时的应用

    jstat工具特别强大,有众多的可选项,详细查看堆内各个部分的使用量,以及加载类的数量.使用时,需加上查看进程的进程id,和所选参数. 执行:cd $JAVA_HOME/bin中执行jstat,注意j ...

  9. 浅谈压缩感知(二十八):压缩感知重构算法之广义正交匹配追踪(gOMP)

    主要内容: gOMP的算法流程 gOMP的MATLAB实现 一维信号的实验与结果 稀疏度K与重构成功概率关系的实验与结果 一.gOMP的算法流程 广义正交匹配追踪(Generalized OMP, g ...

  10. dns简介

    dns(domain name system),它是提供域名到ip的解析功能的系统.它和普通的系统一样,也是运行在服务器之上的. 1.dns指定的ip是用来干嘛的? 这个ip指向dns系统所在的机器. ...