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 类来 ...
随机推荐
- [知识点]网络流之Dinic算法
// 此博文为迁移而来,写于2015年2月6日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vrg4.html ...
- [Cocos2D-x For WP8]Tile Map创建地图
在Cocos2D-x里面创建Tile Map地图是需要用到.tmx的地图文件的,那么创建Tile Map地图文件,我们可以通过地图编辑器来创建,地图编辑器可以在网站:http://www.mapedi ...
- 【BZOJ】1436: Poi2003 Trinomial
题意 \(q(1 \le q \le 10000)\)次询问,每一次求\((x^2+x+1)^n\)的第\(k\)项系数模3. 分析 听说正解是\(\binom{2n}{m} (m \% 2+1)\) ...
- 【hdu2795】Billboard
Problem Description At the entrance to the university, there is a huge rectangular billboard of size ...
- JavaScript放置位置区别
JavaScript放置位置区别 页面中的脚本会在页面载入浏览器后立即执行.我们并不总希望这样.有时,我们希望当页面载入时执行脚本,而另外的时候,我们则希望当用户触发事件时才执行脚本. 位于 head ...
- SVN错误:Attempted to lock an already-locked dir
在eclipse里提交和更新文件是报错: Attempted to lock an already-locked dir svn: Working copy 'F:\workspace\WebFram ...
- Eclipse 使用maven创建Dynamic Web Project
今天看到spring的官网首页已经没有下载连接了,建议使用maven构建spring项目.虽然maven已经不是新技术,但是这个我还没用过,谷歌走起! 通常创建项目都是用Eclipse,前几年还是My ...
- LUA 配置 运行 异常的备忘录
1. 抛异常“lua: LuaInterface: cannot instantiate interpreter”,如图: 目前,重新生成dll之后,会多生成一个lua51.dll到\Lua\5.1\ ...
- webdriver中处理alert
1 定义isAlertPresent()供调用: public boolean isAlertPresent() { try { driver.switchTo().alert(); re ...
- How to pull Android database to local file system
>adb shell# ls /data/data/PACKAGE_NAME/databases# exit// pull it>adb pull /data/data/PACKAGE_N ...