怎么把自己的配置文件配置到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. 【POJ】3243 Clever Y

    http://poj.org/problem?id=3243 题意:求$a^y \equiv b \pmod{p}$最小的$y$.(0<=x, y, p<=10^9) #include & ...

  2. Android --自定义简单Toast

    1. 效果图

  3. IOS面试题总结

    iOS面试题: 一:网络理论知识的理解 1:Internet物理地址和IP地址转换采用什么协议 ARP(Address Resolution Protocol)地址解析协议 2:Internet采用哪 ...

  4. nodejs模仿http请求组件nodegrass简单例子

    1.搭建nodejs环境. 2.执行npm install nodegrass命令. 3.引入模块,var ng= require(nodegrass); 4.下面先看nodegrass底层的get方 ...

  5. webdriver中PDF控件无法显示的问题(IE兼容性)

    公司的的系统只能运行在32位的IE上,开始从http://selenium-release.storage.googleapis.com/index.html?path=2.48/ 这个路径下去下载了 ...

  6. (JavaScript 2.0: The Complete Reference, Second Edition)javascript 2.0完全手册第二版 翻译说明

    1,译文中javascript简称js. 2,本人翻译时将信息提炼加工,保留主要信息,个别地方可能与原文有出入. 3,为督促自己学习Javascript,从今天起每天翻译一些,每天更新. 下面是文章每 ...

  7. CAS单点登录中文用户名乱码问题

    CAS单点登录中文用户名乱码问题,有两种情况 1. CAS server乱码 即在向server端提交用户名和密码时,发生了乱码,解决方法是: 打开WEB-INF/web.xml,在其它的Filter ...

  8. IE hack中主要的几个

    _: IE6; #*+.: IE6 IE7; black\0: IE8; black\9: IE所有; @media screen\9 { … }: IE6 IE7; @media \0screen\ ...

  9. ZK 代码自动提示

    1.设置xsd 打开eclipse,Window-Preference,进行如下设置: 2.创建zul文件 (1)打开File—New—Other窗口,新建XML File文件: (2)选择新建文件所 ...

  10. zju(4)使用busybox制作根文件系统

    1.实验目的 1.学习和掌握busybox相关知识及应用: 2.学会使用交叉编译器定制一个busybox: 3.利用该busybox制作一个文件系统: 4.熟悉根文件系统组织结构: 5.定制.编译ra ...