c# ConfigurationSection
怎么把自己的配置文件配置到app.config中?
- 方案1:在app.config中添加
<!--应用配置-->
<appSettings configSource="Conf\AppSettings.config" />
如果我需要自己定义单独的多个配置文件,又该怎么处理才可以把配置文件添加app.config中呢?
- 方案2:定义ConfigurationSection类
MyConfigurationSection
public class MyConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("", IsDefaultCollection = true)]
public KeyValueConfigurationElementCollection My
{
get { return (KeyValueConfigurationElementCollection)base[""]; }
} public string GetValueByKey(string key)
{
foreach (KeyValueConfigurationElement item in this.My)
{
if (item.Key == key)
return item.Value;
} return string.Empty;
} //[ConfigurationProperty("configSource", IsRequired = false)]
//public string ConfigSource
//{
// get { return (string)base["configSource"]; }
// set
// {
// base["configSource"] = value;
// }
//}
}
KeyValueConfigurationElementCollection
public class KeyValueConfigurationElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new KeyValueConfigurationElement();
} protected override object GetElementKey(ConfigurationElement element)
{
return (element as KeyValueConfigurationElement).Key;
} public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
} protected override string ElementName
{
get { return "add"; }
}
}
KeyValueConfigurationElement
public class KeyValueConfigurationElement : ConfigurationElement
{
[ConfigurationProperty("key", IsRequired = true)]
public string Key
{
get { return (string)base["key"]; }
set { base["key"] = value; }
} [ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get { return (string)base["value"]; }
set { base["value"] = value; }
}
}
修改app.config配置文件:
<configSections>
<section name="my" type="DTCore.MyConfigurationSection,DTCore"/>
</configSections> <mre configSource="Conf\My_Settings.config" />
Conf\My_Settings.config
<?xml version="1.0" encoding="utf-8"?>
<my>
<add key="DT.AKey" value="c key"/>
<add key="DT.CKey.RIPPRB" value="the value"/>
</my>
怎么调用:
MyConfigurationSection mySection=(MyConfigurationSection)ConfigurationManager.GetSection("my");
string value=mySection.GetValueByKey("DT.AKey");
c# ConfigurationSection的更多相关文章
- ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节集合
核心代码 using System; using System.Data; using System.Configuration; using System.Web; using System.Web ...
- ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节
主要代码,一定要继续System.Configuration.ConfigurationSection,具体的节点名称可以自行修改 using System; using System.Data; u ...
- 自定义ConfigurationSection,创建多个嵌套的ConfigurationElementCollection节点
由于接口地址都是固定的,所以想到使用自定义节点,来将接口都配置到web.config中. 很快,v1.0版本出炉: public class RequestConfigSection : Config ...
- .Net 配置文件--继承ConfigurationSection实现自定义处理类处理自定义配置节点
除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...
- .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点
除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...
- [转]通过继承ConfigurationSection,在web.config中增加自定义配置
本文转自:http://www.blue1000.com/bkhtml/2008-02/55810.htm 前几天写了一篇使用IConfigurationSectionHandler在web.conf ...
- .Net 配置文件——继承ConfigurationSection实现自己定义处理类处理自己定义配置节点
除了使用继承IConfigurationSectionHandler的方法定义处理自己定义节点的类.还能够通过继承ConfigurationSection类实现相同效果. 首先说下.Net配置文件里一 ...
- [2014-02-19]ConfigurationSection:让web.config配置更有条理
本文针对新手 使用Web.config的配置信息,一般都习惯于使用 ConfigurationManager.AppSettings["ConfigKey"] 当程序不断迭代,开发 ...
- 使用 ConfigurationSection 创建自定义配置节
我们可以通过用自己的 XML 配置元素来扩展标准的 ASP.NET 配置设置集,要完成这一功能,我们必须实现继承System.Configuration.ConfigurationSection 类来 ...
随机推荐
- jQueryUI日期显示
<script type="text/javascript" src="js/My97DatePicker/WdatePicker.js">< ...
- jsonkit mrc于arc混编
- UIButton在Disabled状态下标题混乱的问题
最近开发中遇到的问题汇总 有段时间没有归纳开发中遇到的一些问题了,今天就写一下之前开发中遇到的几个问题.希望这 篇文章能让读者在以后的开发中少走弯路.本文将依次介绍<UIButton在Disab ...
- Flex——Array,ArrayCollection,Vector性能比较(转)
测试方法 private function Test():void { ;j<;j++) { trace("插入10000项============"); var t1:in ...
- CentOS默认开放的本地端口范围
系统本地开放端口的范围:(默认30000多到60000多) [root@linux2 ~]# vim /etc/sysctl.conf net.ipv4.ip_local_port_range = 1 ...
- HDU 3584 树状数组
Cube Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submi ...
- HDU1176
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- Linux_shell脚本_遍历文件夹下所有文件
参考:lunar1983的专栏 实现:从给定目录树中grep出含制定字符串的行,并给出所在路径 代码如下所示: #!/bin/sh - if [ $# -ne 2 ] then echo " ...
- vue 一些开发姿势
.vue : <template></template><script></script> .js :import Vue from 'vue'
- 阶乘之和--nyoj91
描述 给你一个非负数整数n,判断n是不是一些数(这些数不允许重复使用,且为正数)的阶乘之和,如9=1!+2!+3!,如果是,则输出Yes,否则输出No: 输入 第一行有一个整数0<m<10 ...