zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

在web.config和app.config文件中增加自定义配置节点的方法

2023-06-13 09:15:16 时间

有经验的开发人员都知道在开发.NET应用时可以利用配置文件保存一些常用并且有可能变化的信息,例如日志文件的保存路径、数据库连接信息等等,这样即使生产环境中的参数信息与开发环境不一致也只需要更改配置文件而不用改动源代码再重新编译,极其方便。并且我们一般还约定,在<appSettings>节点保存应用程序的配置信息,在<connectionStrings>中保存数据库连接字符串信息。

上面的这些方法和约定足以让我们在大部分开发中获得方便,但是在有些情况下有些配置信息可以按组分类存放,如果采用上面的方法不仅不直观,而且读取起来也不是太方便,幸好在.NET里就提供了这样的方法。如果有使用过Log4Net或者Enyim.Caching的朋友,肯定对下面的配置不会陌生:

复制代码代码如下:

<sectionGroupname="enyim.com"><sectionname="memcached"
type="Enyim.Caching.Configuration.MemcachedClientSection,Enyim.Caching"/></sectionGroup>

或:
复制代码代码如下:

<configSections><sectionname="log4net"type="System.Configuration.IgnoreSectionHandler"/></configSections>

在出现上面配置的配置文件中,我们就会找到名称为"enyim.com"或者"log4net"的节点,尽管它们本不属于config文件的默认节点,但是通过上面的配置之后程序运行并不会报错。这样一来,相关配置信息也可以很好分类保存起来。

在这里我演示一个简单的例子,这个例子来源于我的一个从2006年起就开始开发的自用软件(因为没有美化所以没有免费发布),在这个应用程序的connfig文件中我增加了一些特有的配置,所以新增了一个自己的节点,app.config文件内容如下:
复制代码代码如下:
<?xmlversion="1.0"encoding="utf-8"?>
<configuration>  
  <configSections>      
   <sectionname="SoftwareSettings"type="ImageAssistant.Configuration.SoftwareSettings,ImageAssistant"/>  
  </configSections>  
    <SoftwareSettings>    
      <LoadSettings>      
        <addkey="LoadBmp"value="true"/>      
        <addkey="LoadJpg"value="true"/>      
        <addkey="LoadGif"value="true"/>      
        <addkey="LoadPng"value="false"/>    
      </LoadSettings>    
    <PathSettingsSavePath="C:\ResizeImages\"SearchSubPath="true"/>  
  </SoftwareSettings>  

  <appSettings>    
     <addkey="LoadBmp"value="true"/>    
     <addkey="LoadJpg"value="true"/>    
     <addkey="LoadGif"value="true"/>    
     <addkey="LoadPng"value="false"/>   
     <addkey="IncludeSubPath" value="true"/> 
  </appSettings>    
</configuration>


在config文件中我们使用<sectionname="SoftwareSettings"type="ImageAssistant.Configuration.SoftwareSettings,ImageAssistant"/>告诉应用程序对于配置文件中的SoftwareSettings节点,其对应的类是ImageAssistant程序集中ImageAssistant.Configuration.SoftwareSettings类,并且在<SoftwareSettings>节点中我们还看到有<LoadSettings>节点和<PathSettings>节点,其中<LoadSettings>是一个节点集合,还包含有多个子节点,为了表示清楚这些关系我们需要添加四个类:SoftwareSettings、LoadSettingsCollection、LoadSettingsElement及PathSettingElement。为了发布方便,我将这四个类的代码放在一个物理文件中,代码如下(注意添加对System.Configuration.dll的引用):
复制代码代码如下:
usingSystem; 
usingSystem.Collections.Generic; 
usingSystem.Linq; 
usingSystem.Text; 
usingSystem.Configuration; 

namespaceImageAssistant.Configuration 

   publicsealedclassLoadSettingsCollection:ConfigurationElementCollection 
   { 
       privateIDictionary<string,bool>settings; 

       protectedoverrideConfigurationElementCreateNewElement() 
       { 
           returnnewLoadSettingsElement(); 
       } 

       protectedoverrideobjectGetElementKey(ConfigurationElementelement) 
       { 
           LoadSettingsElementep=(LoadSettingsElement)element; 

           returnep.Key; 
       } 

       protectedoverridestringElementName 
       { 
           get 
           { 
               returnbase.ElementName; 
           } 
       } 

       publicIDictionary<string,bool>Settings 
       { 
           get 
           { 
               if(settings==null) 
               { 
                   settings=newDictionary<string,bool>(); 
                   foreach(LoadSettingsElementeinthis) 
                   { 
                       settings.Add(e.Key,e.Value); 
                   } 
               } 
               returnsettings; 
           } 
       } 

       publicboolthis[stringkey] 
       { 
           get 
           { 
               boolisLoad=true; 
               if(settings.TryGetValue(key,outisLoad)) 
               { 
                   returnisLoad; 
               } 
               else 
               { 
                   thrownewArgumentException("没有对""+key+""节点进行配置。"); 
               } 
           } 
       } 

   } 

   publicclassLoadSettingsElement:ConfigurationElement 
   { 
       [ConfigurationProperty("key",IsRequired=true)] 
       publicstringKey 
       { 
           get{return(string)base["key"];} 
           set{base["key"]=value;} 
       } 
       [ConfigurationProperty("value",IsRequired=true)] 
       publicboolValue 
       { 
           get{return(bool)base["value"];} 
           set{base["value"]=value;} 
       } 
   } 

   publicclassPathSettingElement:ConfigurationElement 
   { 
       ///<summary>  
       ///   
       ///</summary>  
       [ConfigurationProperty("SavePath",IsRequired=true)] 
       publicstringSavePath 
       { 
           get{return(string)base["SavePath"];} 
           set{base["SavePath"]=value;} 
       } 
       ///<summary>  
       ///   
       ///</summary>  
       [ConfigurationProperty("SearchSubPath",IsRequired=false,DefaultValue=true)] 
       publicboolSearchSubPath 
       { 
           get{return(bool)base["SearchSubPath"];} 
           set{base["SearchSubPath"]=value;} 
       } 
   } 

   ///<summary>  
   ///对应config文件中的  
   ///</summary>  
   publicsealedclassSoftwareSettings:ConfigurationSection 
   { 
       ///<summary>  
       ///对应SoftwareSettings节点下的LoadSettings子节点  
       ///</summary>  
       [ConfigurationProperty("LoadSettings",IsRequired=true)] 
       publicLoadSettingsCollectionLoadSettings 
       { 
           get{return(LoadSettingsCollection)base["LoadSettings"];} 
       } 

       ///<summary>  
       ///对应SoftwareSettings节点下的PathSettings子节点,非必须  
       ///</summary>  
       [ConfigurationProperty("PathSettings",IsRequired=false)] 
       publicPathSettingElementPathSetting 
       { 
           get{return(PathSettingElement)base["PathSettings"];} 
           set{base["PathSettings"]=value;} 
       } 

   } 
}

在上面的代码中可以看到ConfigurationProperty这个属性,这是表示对应的属性在config文件中的属性名,IsRequired表示是否是必须的属性,还有DefaultValue表示属性的默认值。初次之外,我们还要注意以下关系:
SoftwareSettings:根节点,继承自ConfigurationSection。
LoadSettingsCollection:子节点集合,继承自ConfigurationElementCollection。
LoadSettingsElement:子节点,继承自ConfigurationElement。
PathSettingElement:子节点,继承自ConfigurationElement。
编写了如下代码之后,我们又该如何使用上面的类呢?其实很简单,如下:
复制代码代码如下:
classProgram 
   { 
       staticvoidMain(string[]args) 
       { 
           SoftwareSettingssoftSettings=ConfigurationManager.GetSection("SoftwareSettings")asSoftwareSettings; 

           foreach(stringkeyinsoftSettings.LoadSettings.Settings.Keys) 
           { 
               Console.WriteLine("{0}={1}",key,softSettings.LoadSettings[key]); 
           } 
           Console.WriteLine("SavePath={0},SearchSubPath={1}",softSettings.PathSetting.SavePath,softSettings.PathSetting.SearchSubPath); 
           Console.ReadLine(); 
       } 
   }

这个程序的运行结果如下:
LoadBmp=True
LoadJpg=True
LoadGif=True
LoadPng=False
SavePath=C:/ResizeImages/,SearchSubPath=True

总结:在上面的config文件中通过<appSettings>也达到了类似的效果,但是通过自定义节点我们可以方便地读取相关的应用程序配置,同时也便于维护。如果在开发过程中遇到本文中类似的情况,不妨采取本文所述的方式。