C#读取自定义的config

今天说下C#读写自定义config文件的各种方法。由于这类文章已经很多,但是大多数人举例子都是默认的在app.confg或者web.config进行读写,而不是一般的XML文件,我主要写的是一般的Xml文件,不是默认路径下的app.config.

通常,我们在.NET开发过程中,会接触二种类型的配置文件:config文件,xml文件。今天我主要演示如何创建自己的自定义的配置节点,而不是介绍如何使用appSetting.

首先来看下配置节点。

 

 1 <?xml version="1.0" ?> 
 2 <configuration> 
 3 <configSections> 
 4 <section name="MySection1" type="ConfigApplicationDemo.MySection1, ConfigApplication" /> 
 5 <section name="MySection2" type="ConfigApplicationDemo.MySection2, ConfigApplication" /> 
 6 <section name="MySection3" type="ConfigApplicationDemo.MySection3, ConfigApplication" /> 
 7 <section name="MySection4" type="ConfigApplicationDemo.MySection4, ConfigApplication" />
 8  </configSections> 
 9 <MySection1 name="Halia" url="http://www.cnblogs.com/halia/">
10 </MySection1> 
11 <MySection2> 
12 <users username="halia" password="test2012"></users> </MySection2> 
13 <MySection3> 
14 <add key="aa" value="11111"></add>
15  <add key="bb" value="22222"></add>
16  <add key="cc" value="33333"></add> 
17 </MySection3> 
18 <MySection4> 
19  <add name="aa" id="11111" role="test"></add>
20  <add name="bb" id="22222" role="key"></add>
21  <add name="cc" id="33333" role="rest"></add> 
22 </MySection4> 
23  </configuration>

 

 

这里大家可以看到我定义了四种不同的section,其中前两种介绍的人很多,这里不多赘述,先看看第三种。

 

<MySection3> 
<add key="aa" value="11111"></add>
<add key="bb" value="22222"></add>
<add key="cc" value="33333"></add> 
</MySection3> 

这个配置具有一系列数据,这时候就要用到collection,代码如下:

public class MySection3 : ConfigurationSection    // 所有配置节点都要选择这个基类
{    
private static readonly ConfigurationProperty s_property = new ConfigurationProperty(string.Empty, typeof(TheKeyValueCollection), null, ConfigurationPropertyOptions.IsDefaultCollection); [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)] public TheKeyValueCollection KeyValues
{
  get {
      return (TheKeyValueCollection)base[s_property];
     } }


[ConfigurationCollection(
typeof(TheKeyValue))]
public class TheKeyValueCollection: ConfigurationElementCollection // 自定义一个集合 { new public TheKeyValuethis[string name]
  { get {
      
return (TheKeyValue)base.BaseGet(name);
  }
}
// 下面二个方法中抽象类中必须要实现的。 protected override ConfigurationElement CreateNewElement()
  {
    return new TheKeyValue();
  }
protected override object GetElementKey
  (
    ConfigurationElement element)
  {
return ((TheKeyValue)element).Key;
  }
}


public class TheKeyValue : ConfigurationElement // 集合中的每个元素 {
  [ConfigurationProperty("key", IsRequired = true)]
  public string Key
  {
get { return this["key"].ToString(); } set { this["key"] = value; }
  }
[ConfigurationProperty(
"value", IsRequired = true)]
  public string Value {
    get { return this["value"].ToString(); } set { this["value"] = value; } }
  }
}

上面的三个class做了三件事:
1. 为每个集合中的参数项创建一个从ConfigurationElement继承的派生类。
2. 为集合创建一个从ConfigurationElementCollection继承的集合类,具体在实现时主要就是调用基类的方法。
3. 在创建ConfigurationSection的继承类时,创建一个表示集合的属性就可以了,注意[ConfigurationProperty]的各参数。

然后就是读取config中的值,在读取自定节点时,我们需要调用ConfigurationManager.GetSection()得到配置节点,并转换成我们定义的配置节点类,然后就可以按照强类型的方式来访问了。

                if (!string.IsNullOrEmpty(ConfigFile))
//这里的ConfigFile是你要读取的Xml文件的路径,如果为空,则使用默认的app.config文件
{
var fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = ConfigFile }; var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); MySection3 configSection = (MySection3 )config.GetSection("MySection3"); } else { //Get the default config app.config MySection3 configSection = (MySection3 )ConfigurationManager.GetSection("MySection3"); }

var values = from kv in configSection.KeyValues.Cast<TheKeyValue>()
                         select new
            { key=kv.Key, val=kv.Value)};

 我们在来看下section4的配置:

<MySection4> 
<add name="aa" id="11111" role="test"></add>
<add name="bb" id="22222" role="key"></add>
<add name="cc" id="33333" role="rest"></add> 
</MySection4> 

这个关于Collection的写法和第三个差不多,不同之处在于属性的定义是不同的。

public class TheKeyValue : ConfigurationElement    // 集合中的每个元素
{    
  [ConfigurationProperty("name", IsRequired = true)]    
  public string Name
  {
        get { return this["name"].ToString(); }
        set { this["name"] = value; }    
  }

    [ConfigurationProperty("id", IsRequired = true)]    
  public string Id
    {        
    get { return this["id"].ToString(); }
        set { this["id"] = value; }    }
  }

    [ConfigurationProperty("role", IsRequired = true)]    
  public string Role
    {        
    get { return this["role"].ToString(); }
        set { this["role"] = value; }    }
  }
}

然后是获取相应的值:

 if (!string.IsNullOrEmpty(ConfigFile))
//这里的ConfigFile是你要读取的Xml文件的路径,如果为空,则使用默认的app.config文件
                {

                    var fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = ConfigFile };
                    var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                   MySection4 configSection = (MySection4 )config.GetSection("MySection4");
                }
                else
                {
                    //Get the default config app.config
                    MySection4  configSection = (MySection4 )ConfigurationManager.GetSection("MySection4");
                }

             var values = from v in configSection.KeyValues.Cast<TheKeyValue>()
                         select new
            { name=v.Name, id=v.Id,role=v.Role)};

上面就是关于自定义section的一些分享,这里还要说一下section里面的type
<section name="MySection1" type="ConfigApplicationDemo.MySection1, ConfigApplication" />

关于这个type的定义,逗号前面ConfigApplicationDemo.MySection1, 需要对应你的namespace和你的section类的名字,逗号后面需要对应的是你编译出来的dll的名字,很多情况下namespace和dll名字是一样的,但是出现不一样的情况一定要注意,不然type定义不好,或版本不对,会报错,错误如下,程序会告诉你找不到指定的file或者section。

An error occurred creating the configuration section handler for XXX: Could not load type..

 

 

 

 

posted on 2013-06-25 15:54  halia  阅读(7092)  评论(0编辑  收藏  举报