config中自定义配置
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中自定义配置的更多相关文章
- 项目文件中含有两个config文件,app.config与app1.config,如何获取app1.config中的配置
想要通过配置文件配置C#前台画面,好奇做了以下测试:在项目中新建了app.config与app1.config两个配置文件,请教一下各位高手如果想从app1.config中读取配置信息应该如何读取?采 ...
- [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法
本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...
- web.config or app.config 中configSections配置节点
以前还真没见过,今天看项目中有在用,简单写了个Demo,这样配置的好处就是可以自定义配置,更加模块化,直接上代码; 1.配置文件 由于我创建的是一个控制台项目,所以配置文件是App.Config:(这 ...
- C# 中自定义配置
微软在ConfigurationManager类里面为我们提供了AppSetting和ConnectionStrings 两个常用配置, 但是有时候我们需要自定的配置,例如 <image lef ...
- c# 操作.config中AppSettings配置节
ConfigurationSettings.AppSettings[key].ToString(); 这种方式很眼熟吧? 不过这种方式基本过时了,虽然还能用. 微软建议采用ConfigurationM ...
- ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节
主要代码,一定要继续System.Configuration.ConfigurationSection,具体的节点名称可以自行修改 using System; using System.Data; u ...
- EntityFramework在root目录web.config中的配置设置
未找到具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序的实体框架提供程序.请确保在应用程序配置文件的“entityFramework”节中注册了该提供程序.有关详 ...
- springboot读取application.properties中自定义配置
假设在application-xxx.properties中配置 user.name=yuhk 一.在Controller中读取 @Value("{$user.name}") pr ...
- ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节集合
核心代码 using System; using System.Data; using System.Configuration; using System.Web; using System.Web ...
随机推荐
- python 多线程 笔记(一)
#coding=utf-8 import threading from time import sleep, ctime loops = [4,2] def loop(nloop, nsec): pr ...
- Zabbix3.0 自动邮件报障
Zabbix3.0以后,自带的邮件报警支持SSL验证了, 但是仍然没有发送复数个邮箱以及CC,BCC的功能, 因此,我们还是得用别的方法来实现邮件报障. 实现方法有很多种,我用的是PHPmailer. ...
- ASP.NET MVC学习之模型绑定(1)
一.前言 下面我们将开始学习模型绑定,通过下面的知识我们将能够理解ASP.NET MVC模型的模型绑定器是如何将http请求中的数据转换成模型的,其中我们重点讲述的是表单数据. 二.正文 1.简单类型 ...
- Kali Linux Web 渗透测试视频教程— 第八课 nessus
Kali Linux Web 渗透测试视频教程— 第八课 nessus 文/玄魂 视频课程地址:http://edu.51cto.com/course/course_id-1887.html 目录 n ...
- freshcodecolor纯正则实现的在线代码着色(高亮)
小菜最新完成的一款在线代码着色工具-freshcodecolor,该工具采用Javascript编写,着色识别策略完全采用正则表达式,无奈正则表达式在Javascript中有很大局限性,导致某些场合识 ...
- win32汇编hello world
下载:http://www.masm32.com/ 安装masm32 建一个Var.bat文件并运行 @echo offset include=E:\masm32\includeset lib=E:\ ...
- crossplatform---Nodejs in Visual Studio Code 10.IISNode
1.开始 Nodejs in Visual Studio Code 08.IIS : http://www.cnblogs.com/mengkzhaoyun/p/5410185.html 参考此篇内容 ...
- Liferay7 BPM门户开发之28: Portlet文件上传,及实体类同步更新上传
抓住核心 . Liferay文件上传的核心就是使用UploadPortletRequest类 继承关系java.lang.Object extended byjavax.servlet.Servlet ...
- python爬虫抓取数据
URL管理器实现方式:1. 内存python内存待爬取URL集合:set()已爬取URL集合:set() 2. 关系数据库MySQLurls(url, is_crawled) 3. 缓存数据库(高性能 ...
- 正则表达式提取url中的参数,返回json字符串
var urlstr = "www.baidu.com?a=1&b=xx&c"; var s = urlstr.split("?"); var ...