查看完整版本: 讲述如何使用.NET的配置文件

wzq357460772 2007-12-7 09:48

讲述如何使用.NET的配置文件

.NET的应用程序配置文件,使用的是XML格式。相对INI文件来说,它的功能要强上不少,而且具有很强的可扩展性。它的缺点是不能直接进行写操作,也就是说,不能直接在程序中修改配置文件的数据(当然不是指不能,不过不是本文讨论的范围)。本文主要目的是探讨如何扩展配置文件,并在其加入各种自定义配置信息。
   
    1. 使用
        简单的配置信息,可以直接放入标记中。如: [img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]<xml version="1.0" encoding="utf-8"?>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  <appSettings>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img] <add key="LogFile" value="d:\log\debug.log"/>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  appSettings>  
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]configuration>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]
        相应访问代码如下:      
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]string fileName = System.Configuration.ConfigurationSettings.AppSettings.Get("LogFile");
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]
     2. 自定义配置节(section)名称
        比如,我们要使用下面的配置结构,将配置信息归类分组:
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img][img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]<configuration>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  需要在此处加入自定义配置声明 -->
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  以下是自定义配置的内容 -->
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]<myConfig>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  <myDictionary>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]    <add key="Area" value="Fuzhou"/>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]    <add key="Device" value="Printer"/>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]    <add key="Customer" value="Muf"/>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  myDictionary>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  <myNameValue>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]    <add key="Area" value="Fuzhou"/>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]    <add key="Device" value="Printer"/>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]    <add key="Customer" value="Muf"/>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  myNameValue>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  <myInfo
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]    Area="Fuzhou" Device="Printer" Customer="Muf"
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  />
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]myConfig>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]configuration>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]
        但是光这样子说明是不行的。没有声明,是不能使用自定义的配置段。我们必须要在配置文件前面加入声明:  
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  以下是自定义配置的声明 -->
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  <configSections>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]    <sectionGroup name="myConfig">
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]         <section name="myDictionary"
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]            type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]        <section name="myNameValue"
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]            type="System.Configuration.DictionarySectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]        <section name="myInfo"
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]            type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]    sectionGroup>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  configSections>  
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]
    声明和配置的关系,示意图如下:
[img]http://www.qqread.com/ArtImage/20070606/sn59_2.gif[/img]
点击查看大图
        
    由图上可以看出,NameValueSectionHandler和DictionarySectionHandler在定义配置文件的内容形式上是一样的,都是用来设置内容的。只是返回到C#中的类不太一样,可以参考下面的代码示例。
    另外,如果不关心Handler类的版本等信息,可以直接省略。如NameValueSectionHandler可以直接如下声明:

[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]<section name="myDictionary"            type="System.Configuration.NameValueSectionHandler, System" />
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]

wzq357460772 2007-12-7 09:48

把上面的声明段放入配置文件中,我们的配置结构就可以正常使用了。声明中,用来定义不含配置数据的节的名称。用来定义含有自定义配置数据的节的名称。用来指定定义配置数据的类型。.NET已经定义了3种配置类型:
  a. NameValueSectionHandler
        相应访问代码如下:
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]NameValueCollection myNameValue= (NameValueCollection)System.Configuration.ConfigurationSettings.AppSettings.Get(@"myConfig\myNameValue");
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]string Area = myNameValue["Area"];
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]string Device= myNameValue["Device"];
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]string Customer = myNameValue["Customer "];
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]
  b. DictionarySectionHandler
        相应访问代码如下:
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]Hashtable myNameValue= (Hashtable)System.Configuration.ConfigurationSettings.AppSettings.Get(@"myConfig\myDictionary");
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]string Area = myNameValue["Area"];
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]string Device= myNameValue["Device"];
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]string Customer = myNameValue["Customer "];
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]
  c. SingleTagSectionHandler
        相应访问代码如下:   
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]Hashtable myNameValue= (Hashtable)System.Configuration.ConfigurationSettings.AppSettings.Get(@"myConfig\myInfo");
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]string Area = myNameValue["Area"];
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]string Device= myNameValue["Device"];
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]string Customer = myNameValue["Customer "];
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]
        这三种类型的详细信息,可以参考 MSDN 文档。同时.NET 还定义了IgnoreSectionHandler类型,为 System.Configuration 之外的系统所读取和处理的配置节提供节处理程序定义。
        除此之外,.NET提供了IConfigurationSectionHandler接口,这样我们还可以自行进行扩展,以设计出我们自已的配置形式。


        3. 自定义配置结构 (使用IConfigurationSectionHandler)
假设有以下的配置信息,其在MyInfo可以重复许多次,那么应如何读取配置呢?这时就要使用自定义的配置程序了。
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]<myConfigs>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  <myInfo Area="Fuzhou" Device="Printer" Customer="Muf" />
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  <myInfo Area="Shanghai" Device="Mobile" Customer="Liny" />
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]myConfig>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]
访问代码如下:
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]Hashtable cfgTable = (Hashtable)ConfigurationSettings.GetConfig( "myConfigs" );
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]Debug.Assert( cfgTable.Count == 2);
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]Hashtable cfgFuzhou = (Hashtable)cfgTable["Fuzhou"];
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]Hashtable cfgShanghai = (Hashtable)cfgTable["Shanghai"];
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]Debug.Assert( cfgFuzhou["Device"] == "Printer" );
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]Debug.Assert( cfgShanghai["Device"] == "Mobile" );
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]Debug.Assert( cfgFuzhou["Customer"] == "Muf" );
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]Debug.Assert( cfgShanghai["Customer"] == "Liny" );
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]foreach(Hashtable cfg in cfgTable.Values)
[img]http://www.qqread.com/ArtImage/20070606/sn59_3.gif[/img][img]http://www.qqread.com/ArtImage/20070606/sn59_4.gif[/img][img]http://www.qqread.com/ArtImage/20070606/sn59_5.gif[/img]{
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img] Console.WriteLine("Area={0} Device={1} Customer={2}", cfg["Area"], cfg["Device"], cfg["Customer"]);
[img]http://www.qqread.com/ArtImage/20070606/sn59_7.gif[/img]}
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]
为了能使用上面的访问代码来访问配置结构,我们需要生成一个特定的配置读取类(ConfigurationSectionHandler),例子很简单,就不多做说明了:
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]public class MyInfoSectionHandler: IConfigurationSectionHandler
[img]http://www.qqread.com/ArtImage/20070606/sn59_3.gif[/img][img]http://www.qqread.com/ArtImage/20070606/sn59_4.gif[/img][img]http://www.qqread.com/ArtImage/20070606/sn59_5.gif[/img]{
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img] public object Create(object parent, object configContext, System.Xml.XmlNode section)
[img]http://www.qqread.com/ArtImage/20070606/sn59_8.gif[/img][img]http://www.qqread.com/ArtImage/20070606/sn59_9.gif[/img] [img]http://www.qqread.com/ArtImage/20070606/sn59_5.gif[/img]{
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img]  Hashtable config = new Hashtable();
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img]  foreach(XmlNode node in section.ChildNodes)
[img]http://www.qqread.com/ArtImage/20070606/sn59_8.gif[/img][img]http://www.qqread.com/ArtImage/20070606/sn59_9.gif[/img]  [img]http://www.qqread.com/ArtImage/20070606/sn59_5.gif[/img]{
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img]   if(node.Name != "myInfo")
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img]    throw new System.Configuration.ConfigurationException("不可识别的配置项", node);
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img]
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img]   Hashtable item = new Hashtable();
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img]   foreach(XmlAttribute attr in node.Attributes)
[img]http://www.qqread.com/ArtImage/20070606/sn59_8.gif[/img][img]http://www.qqread.com/ArtImage/20070606/sn59_9.gif[/img]   [img]http://www.qqread.com/ArtImage/20070606/sn59_5.gif[/img]{
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img]    switch(attr.Name)
[img]http://www.qqread.com/ArtImage/20070606/sn59_8.gif[/img][img]http://www.qqread.com/ArtImage/20070606/sn59_9.gif[/img]    [img]http://www.qqread.com/ArtImage/20070606/sn59_5.gif[/img]{
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img]     case "Area":
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img]     case "Device":
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img]     case "Customer":
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img]      item.Add(attr.Name, attr.Value);
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img]      break;
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img]     default:
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img]      throw new System.Configuration.ConfigurationException("不可识别的配置属性", attr);
[img]http://www.qqread.com/ArtImage/20070606/sn59_10.gif[/img]    }
[img]http://www.qqread.com/ArtImage/20070606/sn59_10.gif[/img]   }
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img]   config.Add(item["Area"], item);
[img]http://www.qqread.com/ArtImage/20070606/sn59_10.gif[/img]  }
[img]http://www.qqread.com/ArtImage/20070606/sn59_6.gif[/img]  return config;
[img]http://www.qqread.com/ArtImage/20070606/sn59_10.gif[/img] }
[img]http://www.qqread.com/ArtImage/20070606/sn59_7.gif[/img]}
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]
然后,我们再定义配置说明。其中,myNamespace.MyInfoSectionHandler 是MyInfoSectionHandler类的带名字空间的完整名称;myApp 则是定义MyInfoSectionHandler类的程序集不带扩展名的名字(如myApp.dll或myApp.exe):
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img][img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]<configuration>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]    以下是自定义配置的声明 -->
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  <configSections>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]      <section name="myConfig" type="myNamespace.MyInfoSectionHandler, myApp" />
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  configSections>   
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  <myConfigs>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]    <myInfo Area="Fuzhou" Device="Printer" Customer="Muf" />
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]    <myInfo Area="Shanghai" Device="Mobile" Customer="Liny" />
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]  myConfig>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]configuration>
[img]http://www.qqread.com/ArtImage/20070606/sn59_1.gif[/img]
根据上面的例子,我们可以使用IConfigurationSectionHandler来实现任意的配置文件结构。
页: [1]

查看完整版本: 讲述如何使用.NET的配置文件