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 ...
随机推荐
- 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)
Charm Bracelet POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...
- Mac中brew的安装
brew是Mac OS的一个软件包管理工具,使用简单方便,就像ubuntu中的apt-get命令一样官方地址:http://brew.sh/index_zh-cn.html 终端下运行 /usr/bi ...
- Data层相关问题 & JS循环取值
第一次写博客,里面是自己工作中碰到的问题及总结的知识点,便于自己以后回顾,技术大牛们请直接忽略这篇文章,也希望能帮助到想我这样的小白! Data层相关问题总结: 1. 代码管理用的是 VSS 2005 ...
- 利用Formdata实现form提交文件上传不跳转页面
作者:幻月九十链接:https://www.zhihu.com/question/19631256/answer/119911045来源:知乎著作权归作者所有,转载请联系作者获得授权. $('form ...
- CentOS7 安装 mongodb
https://docs.mongodb.com/manual/tutorial/install-mongodb-enterprise-on-red-hat/
- [游戏模版12] Win32 稳定定时
>_<:The last time,we learned how to use timer to make the picture run and change show,but some ...
- 阻塞队列--LinkedBlockingQueue
什么叫线程安全?线程安全就是每次运行结果和单线程运行的结果是一样的,而且其他的变量的值也和预期的是一样的. 线程安全就是说多线程访问同一代码,不会产生不确定的结果. 并行和并发区别1.并行是指两者同时 ...
- windows下安装mysql压缩包版[转]
版本:5.6.17 1.将解压后的文件夹放到某个目录下,比如c:\software; 2.在环境变量中新建MYSQL_HOME=C:\software\mysql-5.6.17-winx64,然后在系 ...
- 成功安装mysql后,为何服务管理器里找不到MYSQL服务名
1.打开cmd,切换到mysql的bin目录下 2. D:\Program Files\MySQL5.1\bin>mysqld.exe -installService successfully ...
- paip.slap工具与于64位win7与JDBC的性能对比
paip.slap工具与于64位win7与JDBC的性能对比 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog ...