深入理解.NET配置文件

      在.Net中,配置文件实际分为了两部分,一部分是配置的实际内容,比如appSettings以及上例中的blogforum结点;另一部分指定结点的处理程序,这些结点位于 configSections 结点下面。

    也就是说configSections下面是定义了对配置文件中。处了configSections节点意外的节点的处理程序。之所以这样是有原因的,我们都知道在配置文件下面appSettings节点是这样的形式:

     <add key="IV" value="kJlv8Uk0BEngaZL4fQGN6A=="/>

      而对于system.web节点却比这个要复杂的多。之所以造成这样的差异是由于不同节点所起的配置功能的不同而造成的,因此对于节点结构的不同,必须要有不同的处理程序来读取这些配置节点中的属性或者值。而定义这些节点的处理程序就是在configSections节点下面进行定义的。

      也许你会惊异,默认情况下Web.config文件下是没有configSections节点的。这是因为对于配置文件下的节点.NET都已经有了其读取节点的处理程序。而配置这些节点处理程序就在machine.config文件下面。例如:

      appSettings节点 

      <section name="appSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false"/>
     

      connectionStrings节点  

      <section name="connectionStrings" type="System.Configuration.ConnectionStringsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="false"/>

      system.web节点,由于该节点较为复杂,其下面有很多子节点。因此用sectionGroup,为了简练这里中间省略掉很多相同的代码。

      

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
< sectionGroup name ="system.web" type ="System.Web.Configuration.SystemWebSectionGroup, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" >
< section name ="anonymousIdentification" type ="System.Web.Configuration.AnonymousIdentificationSection, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" allowDefinition ="MachineToApplication" />
< section name ="authentication" type ="System.Web.Configuration.AuthenticationSection, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" allowDefinition ="MachineToApplication" />
< section name ="authorization" type ="System.Web.Configuration.AuthorizationSection, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
< sectionGroup name ="caching" type ="System.Web.Configuration.SystemWebCachingSectionGroup, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" >
< section name ="cache" type ="System.Web.Configuration.CacheSection, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" allowDefinition ="MachineToApplication" />
< section name ="outputCache" type ="System.Web.Configuration.OutputCacheSection, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" allowDefinition ="MachineToApplication" />
< section name ="outputCacheSettings" type ="System.Web.Configuration.OutputCacheSettingsSection, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" allowDefinition ="MachineToApplication" />
< section name ="sqlCacheDependency" type ="System.Web.Configuration.SqlCacheDependencySection, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" allowDefinition ="MachineToApplication" />
</ sectionGroup >
</ sectionGroup >

 

        .NET框架就是通过配置文件的灵活配置,以及这些配置文件的处理程序,和设计模式一起组合使其强大灵活可扩展。

  对于.NET配置文件有三个重要的类。ConfigurationManager,Configuration,ConfigurationSection.

  ConfigurationManager:

        提供对客户端应用程序配置文件的访问。

        AppSettings属性可以访问当前应用程序配置的appSetting。

        ConnectionStrings 访问配置的ConnectionStringsSection 数据。

        public static object GetSection(string sectionName)  检索当前应用程序默认配置的指定配置节,如果配置节指定的dll不在当前项目内会出现错误。

  Configuration:表示应用程序的配置文件,也就是对应用程序配置的封装。此类没有构造函数,但是可以通过ConfigurationManger的open方法来初始化此类的实列

  ConfigurationSection:表示configSections配置节的实列。                        

  疑问:可以通过Configuration的Sections和SectionGroups属性来获取当前配置文件所定义的configSections或者sectionGroup。但是有一点觉得奇怪,为什么自定义的遍历不到?

转载于:https://www.cnblogs.com/wuxiaoqian726/articles/1921153.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值