怎么把自己的配置文件配置到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的更多相关文章

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

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

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

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

  3. 自定义ConfigurationSection,创建多个嵌套的ConfigurationElementCollection节点

    由于接口地址都是固定的,所以想到使用自定义节点,来将接口都配置到web.config中. 很快,v1.0版本出炉: public class RequestConfigSection : Config ...

  4. .Net 配置文件--继承ConfigurationSection实现自定义处理类处理自定义配置节点

    除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...

  5. .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点

    除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...

  6. [转]通过继承ConfigurationSection,在web.config中增加自定义配置

    本文转自:http://www.blue1000.com/bkhtml/2008-02/55810.htm 前几天写了一篇使用IConfigurationSectionHandler在web.conf ...

  7. .Net 配置文件——继承ConfigurationSection实现自己定义处理类处理自己定义配置节点

    除了使用继承IConfigurationSectionHandler的方法定义处理自己定义节点的类.还能够通过继承ConfigurationSection类实现相同效果. 首先说下.Net配置文件里一 ...

  8. [2014-02-19]ConfigurationSection:让web.config配置更有条理

    本文针对新手 使用Web.config的配置信息,一般都习惯于使用 ConfigurationManager.AppSettings["ConfigKey"] 当程序不断迭代,开发 ...

  9. 使用 ConfigurationSection 创建自定义配置节

    我们可以通过用自己的 XML 配置元素来扩展标准的 ASP.NET 配置设置集,要完成这一功能,我们必须实现继承System.Configuration.ConfigurationSection 类来 ...

随机推荐

  1. 4分钟apache自带ab压力测试工具使用: 2015.10.4

    2015.10.44分钟apache自带ab压力测试工具使用:win8.1 wampserver2.5 -Apache-2.4.9-Mysql-5.6.17-php5.5.12-64b 可以参考一下部 ...

  2. Command mvn jetty:run

    How to use the command mav jetty:run? There is a simple display as below: Step 1: You should generat ...

  3. spin_lock 和 spin_lock_irqsave

    一  .spin_lock_irqsave . spin_unlock_irqrestore 如果自旋锁在中断处理函数中被用到,那么在获取该锁之前需要关闭本地中断,spin_lock_irqsave ...

  4. 简单 常用的git命令

    常用的git命令 git pull  获取最新 git add . 提交所有 git commit  -m “我的注释” git status 查看状态 git push origin master ...

  5. 20145330第六周《Java学习笔记》

    20145330第六周<Java学习笔记> . 这周算是很忙碌的一周.因为第六周陆续很多实验都开始进行,开始要准备和预习的科目日渐增多,对Java分配的时间不知不觉就减少了,然而第十和十一 ...

  6. 腾讯 pc端面试(2015.10.26)

    整个面试过程全部围绕着我在前一家公司做过的项目开始提问.因为这个项目是我主要负责的,所以面试官第一个问题是让我讲解了整个项目的框架结构.在对于项目的框架结构的讲解方面,大致条理还算清醒但是因为很少对做 ...

  7. 看好你的门-客户端传数据-用java修改referer

    1.简单说明 Referer.origin用来表明,浏览器向WEB服务器表明自己来自哪里.但是就它本身而言,并非完全安全. 写一个例子,可以任意修改http信息头中的referer.origin 2. ...

  8. 移位运算符(JAVA)

    java中有三种移位运算符 <<      :     左移运算符,num << 1,相当于num乘以2 >>     :     右移运算符,num >&g ...

  9. PLSQL Developer注册码

    Product Code:4t46t6vydkvsxekkvf3fjnpzy5wbuhphqzserial Number:601769password:xs374ca

  10. php二维数组的取值与转换

    while(list($key,$value) = each($arr)) { while(list($k,$v) = each($value)) { echo $key."==>&q ...