NET基础课--配置文件2

 1. 使用<appSettings>
        简单的配置信息,可以直接放入<appSettings>标记中。如:
<?xml version="1.0" encoding="utf-8"?>
   <configuration>
    <appSettings>
      <add key="LogFile" value="d:logdebug.log"/>
   </appSettings>  
</configuration>

        相应访问代码如下:       

string fileName = System.Configuration.ConfigurationSettings.AppSettings.Get("LogFile");

     2. 自定义配置节(section)名称
        比如,我们要使用下面的配置结构,将配置信息归类分组:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- 需要在此处加入自定义配置声明 -->
<!-- 以下是自定义配置的内容 -->
<myConfig>
  <myDictionary>
    <add key="Area" value="Fuzhou"/>
    <add key="Device" value="Printer"/> 
    <add key="Customer" value="Muf"/>
  </myDictionary>
  <myNameValue>
    <add key="Area" value="Fuzhou"/>
    <add key="Device" value="Printer"/> 
    <add key="Customer" value="Muf"/>
  </myNameValue>
  <myInfo  Area="Fuzhou" Device="Printer" Customer="Muf" />
</myConfig>
</configuration>

但是光这样子说明是不行的。没有声明是不能使用自定义的配置段。我们必须要在配置文件前面加入声明:  

<!-- 以下是自定义配置的声明 -->
  <configSections>
    <sectionGroup name="myConfig">
         <section name="myDictionary"
            type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <section name="myNameValue"
            type="System.Configuration.DictionarySectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <section name="myInfo"
            type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </sectionGroup>
  </configSections>  
type="System.Configuration.NameValueSectionHandler, System
type含义是代表处理的类名是system命名空间下configuration下的NameValueSectionHandle类,它所在程序集是system

    声明和配置的关系,示意图如下:
        
    可以看出,NameValueSectionHandler和DictionarySectionHandler在定义配置文件的内容形式上是一样的,都用<add>来设置内容的。只是返回到C#中的类不太一样
    另外,如果不关心Handler类的版本等信息,可以直接省略。如NameValueSectionHandler可以直接如下声明:

<section name="myDictionary"            type="System.Configuration.NameValueSectionHandler, System" />

    把上面的<configSections>声明段放入配置文件中,我们的配置结构就可以正常使用了。声明中,< sectionGroup>用来定义不含配置数据的节的名称。<section>用来定义含有自定义配置数据的节的名称。< section type>用来指定定义配置数据的类型。

    注意,自定义的配置节,不能使用 System.Configuration.ConfigurationSettings.AppSettings.Get 来访问,要使用 System.Configuration.ConfigurationSettings.GetConfig。

    .NET已经定义了3种配置类型:
  a. NameValueSectionHandler

NameValueCollection myNameValue= (NameValueCollection)System.Configuration.ConfigurationSettings.GetConfig(@"myConfig/myNameValue");
string Area = myNameValue["Area"];
string Device= myNameValue["Device"];
string Customer = myNameValue["Customer "];

  b. DictionarySectionHandler

Hashtable myNameValue= (Hashtable)System.Configuration.ConfigurationSettings.GetConfig(@"myConfig/myDictionary");
string Area = myNameValue["Area"];
string Device= myNameValue["Device"];
string Customer = myNameValue["Customer "];

  c. SingleTagSectionHandler

Hashtable myNameValue= (Hashtable)System.Configuration.ConfigurationSettings.GetConfig(@"myConfig/myInfo");
string Area = myNameValue["Area"];
string Device= myNameValue["Device"];
string Customer = myNameValue["Customer "];

        这三种类型的详细信息可以参考 MSDN 文档。同时.NET 还定义了IgnoreSectionHandler类型,为 System.Configuration 之外的系统所读取和处理的配置节提供节处理程序定义。
        除此之外,.NET提供了IConfigurationSectionHandler接口,这样我们还可以自行进行扩展,以设计出我们自已的配置形式。如下例

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!-- 以下是自定义配置的声明 -->
  <configSections>
      <section name="myConfig" type="myNamespace.MyInfoSectionHandler, myApp" />
  </configSections>   
  <myConfig>
    <myInfo Area="Fuzhou" Device="Printer" Customer="Muf" />
    <myInfo Area="Shanghai" Device="Mobile" Customer="Liny" />
  </myConfig>
</configuration>

myConfig部分的内容Framework不能自动去解析,所以我们需要用下面的代码自己实现解析,此处解析到hashtable中了,原本是xmlnode

public class MyInfoSectionHandler: IConfigurationSectionHandler
{
 public object Create(object parent, object configContext, System.Xml.XmlNode section)
 {
  Hashtable config = new Hashtable();
  foreach(XmlNode node in section.ChildNodes)
  {
   if(node.Name != "myInfo")
    throw new System.Configuration.ConfigurationException("不可识别的配置项", node);

   Hashtable item = new Hashtable();
   foreach(XmlAttribute attr in node.Attributes)
   {
    switch(attr.Name)
    {
     case "Area":
     case "Device":
     case "Customer":
      item.Add(attr.Name, attr.Value);
      break;
     default:
      throw new System.Configuration.ConfigurationException("不可识别的配置属性", attr);
    }
   }
   config.Add(item["Area"], item);
  }
  return config;
 }
}

使用GetConfig进行配置文件读取。

Hashtable cfgTable = (Hashtable)ConfigurationSettings.GetConfig( "myConfigs" );

Debug.Assert( cfgTable.Count == 2);
Hashtable cfgFuzhou = (Hashtable)cfgTable["Fuzhou"];
Hashtable cfgShanghai = (Hashtable)cfgTable["Shanghai"];
Debug.Assert( cfgFuzhou["Device"] == "Printer" );
Debug.Assert( cfgShanghai["Device"] == "Mobile" );
Debug.Assert( cfgFuzhou["Customer"] == "Muf" );
Debug.Assert( cfgShanghai["Customer"] == "Liny" );

foreach(Hashtable cfg in cfgTable.Values)
{
 Console.WriteLine("Area={0} Device={1} Customer={2}", cfg["Area"], cfg["Device"], cfg["Customer"]);
}

执行过程:首先ConfigurationSettings的GetConfig方法会去驱动Framework把App.config中名称为myconfig节点内容读取到一个XML的Node里面(实际运用中,我们可以不返回XML节点,而返回自定义的存储结构这样使用起来就更方便,所以代码把它解析到hashtable中)。之后Framework会去自动地调用App.config文件中configSections下标签名即name=myconfig的type下类的Create方法。

原文地址:https://www.cnblogs.com/tiantianle/p/4853147.html