c# ConfigurationSection

怎么把自己的配置文件配置到app.config中?

  • 方案1:在app.config中添加
 <!--应用配置-->
  <appSettings configSource="Conf\AppSettings.config" />

如果我需要自己定义单独的多个配置文件,又该怎么处理才可以把配置文件添加app.config中呢?

  • 方案2:定义ConfigurationSection类
MyConfigurationSection  
 1 public class MyConfigurationSection : ConfigurationSection
 2 {
 3     [ConfigurationProperty("", IsDefaultCollection = true)]
 4     public KeyValueConfigurationElementCollection My
 5     {
 6         get { return (KeyValueConfigurationElementCollection)base[""]; }
 7     }
 8 
 9     public string GetValueByKey(string key)
10     {
11         foreach (KeyValueConfigurationElement item in this.My)
12         {
13             if (item.Key == key)
14                 return item.Value;
15         }
16 
17         return string.Empty;
18     }
19 
20     //[ConfigurationProperty("configSource", IsRequired = false)]
21     //public string ConfigSource
22     //{
23     //    get { return (string)base["configSource"]; }
24     //    set
25     //    {
26     //        base["configSource"] = value;
27     //    }
28     //}
29 }

KeyValueConfigurationElementCollection

 1 public class KeyValueConfigurationElementCollection : ConfigurationElementCollection
 2 {
 3     protected override ConfigurationElement CreateNewElement()
 4     {
 5         return new KeyValueConfigurationElement();
 6     }
 7 
 8     protected override object GetElementKey(ConfigurationElement element)
 9     {
10         return (element as KeyValueConfigurationElement).Key;
11     }
12 
13     public override ConfigurationElementCollectionType CollectionType
14     {
15         get { return ConfigurationElementCollectionType.BasicMap; }
16     }
17 
18     protected override string ElementName
19     {
20         get { return "add"; }
21     }
22 }

KeyValueConfigurationElement

 1 public class KeyValueConfigurationElement : ConfigurationElement
 2 {
 3     [ConfigurationProperty("key", IsRequired = true)]
 4     public string Key
 5     {
 6         get { return (string)base["key"]; }
 7         set { base["key"] = value; }
 8     }
 9 
10     [ConfigurationProperty("value", IsRequired = true)]
11     public string Value
12     {
13         get { return (string)base["value"]; }
14         set { base["value"] = value; }
15     }
16 }

修改app.config配置文件:

<configSections>
    <section name="my" type="DTCore.MyConfigurationSection,DTCore"/>
</configSections>

<mre configSource="Conf\My_Settings.config" />
Conf\My_Settings.config

1 <?xml version="1.0" encoding="utf-8"?>
2 <my>
3   <add key="DT.AKey" value="c key"/>
4   <add key="DT.CKey.RIPPRB" value="the value"/>
5 </my>

怎么调用:

1 MyConfigurationSection mySection=(MyConfigurationSection)ConfigurationManager.GetSection("my");
2 string value=mySection.GetValueByKey("DT.AKey");

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值