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. 安装python 的 包 paramiko

    安装python 的 包 paramiko 安装 依赖 yum -y install gcc python-devel 获取安装 pycryptowget https://pypi.python.or ...

  2. php笔试题(3)--转载

    1.nginx使用哪种网络协议? nginx是应用层 我觉得从下往上的话 传输层用的是tcp/ip 应用层用的是http fastcgi负责调度进程 2. <? echo 'hello tush ...

  3. js 操作map对象

    转自:http://smallvq123.javaeye.com/blog/823923 /* * Map对象,实现Map功能 * * * size() 获取Map元素个数 * isEmpty() 判 ...

  4. C++内存分配方式详解——堆、栈、自由存储区、全局/静态存储区和常量存储区

    栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清除的变量的存储区.里面的变量通常是局部变量.函数参数等.在一个进程中,位于用户虚拟地址空间顶部的是用户栈,编译器用它来实现函数的调用.和堆一样 ...

  5. [转载]& 引用 取地址

    原文地址:& 引用 取地址作者:beter 引用实际上就是给同一个变量取了多个名字.     举个例子:     有个人的名字叫a,之后又改名叫b,这时a和b都是指这个人,这样b就引用了a,即 ...

  6. .net 架构师/经理招聘,长期有效

    岗位职责: 1.参与项目的需求分析和业务评审 2.负责项目的技术架构: 3.负责指导开发小组完成开发以及单元测试: 4.负责解决开发人员的技术问题,并对开发人员的代码进行Review. 任职要求: 1 ...

  7. SRS文档 王倩倩 201303014004

    设计阶段 Spec 图书管理系统functional spec:软件功能说明书, 主要用来说明软件的外部功能, 和用户的交互情况 (把软件当作一个黑盒子).从用户的角度描述软件产品的功能, 输入,输出 ...

  8. 移动Web与js定时器暂停或不准确计时的问题解决

    PC 上的 Firefox.Chrome 和 Safari 等浏览器,都会自动把未激活页面中的 JavaScript 定时器(setTimeout.setInterval)间隔最小值改为 1 秒以上: ...

  9. JS基本内容

    js是网页的脚本语言,它也是有内嵌和外部两种,样式是写在头部,脚本语言可以写在任何位置,通常写在网页底部:<script type="texe/javascript"> ...

  10. 雅虎Yahoo 前段优化 14条军规

    Yahoo 14条 雅虎十四条 腾讯前端设计的Leader推荐我背熟的.请大家都能好好学习,不要像我一样一扫而过,好好的记下来!不仅仅是晓得一些CSS xhtml就好了,深刻认识到很多的东西需要学习的 ...