配置文件在应用程序开发中发挥及其重要的作用,使用配置文件可以动态改变应用程序的程序处理逻辑。
在.net framework 2.0平台中更是使得处理配置文件更加方便、灵活。同时微软也提供了自定义配置文件处理类的功能,

实现自定义配置类主要通过继承ConfigurationSection或者实现IConfigurationSectionHandler
这篇文章介绍的方法是继承 ConfigurationSection的 MySampleConfigurationSection

自定义Net Configuration 类 - 继承ConfigurationSectionusing System;
自定义Net Configuration 类 - 继承ConfigurationSection
using System.Collections.Generic;
自定义Net Configuration 类 - 继承ConfigurationSection
using System.Text;
自定义Net Configuration 类 - 继承ConfigurationSection
自定义Net Configuration 类 - 继承ConfigurationSection
using System.Configuration;
自定义Net Configuration 类 - 继承ConfigurationSection
自定义Net Configuration 类 - 继承ConfigurationSection
namespace ConfigSample

测试的app.config 文件
自定义Net Configuration 类 - 继承ConfigurationSection<?xml version="1.0" encoding="utf-8" ?>
自定义Net Configuration 类 - 继承ConfigurationSection
<configuration>
自定义Net Configuration 类 - 继承ConfigurationSection  
<configSections>
自定义Net Configuration 类 - 继承ConfigurationSection    
<sectionGroup name="mySampleConfigurationSectionGroup">
自定义Net Configuration 类 - 继承ConfigurationSection      
<section name="mySampleConfigurationSection" type="ConfigSample.MySampleConfigurationSection,ConfigSample,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere"/>
自定义Net Configuration 类 - 继承ConfigurationSection    
</sectionGroup>
自定义Net Configuration 类 - 继承ConfigurationSection  
</configSections>
自定义Net Configuration 类 - 继承ConfigurationSection  
<mySampleConfigurationSectionGroup>
自定义Net Configuration 类 - 继承ConfigurationSection    
<mySampleConfigurationSection myAttribute1="MyAttribute1 value">
自定义Net Configuration 类 - 继承ConfigurationSection      
<myChildSection myChildAttribute1="MyChildAttribute1 value" myChildAttribute2="MyChildAttribute2 value" />
自定义Net Configuration 类 - 继承ConfigurationSection    
</mySampleConfigurationSection>
自定义Net Configuration 类 - 继承ConfigurationSection  
</mySampleConfigurationSectionGroup>
自定义Net Configuration 类 - 继承ConfigurationSection
</configuration>

测试该Configuration代码
自定义Net Configuration 类 - 继承ConfigurationSection    // get sample configuration section
自定义Net Configuration 类 - 继承ConfigurationSection
            MySampleConfigurationSection myConfigSection = (MySampleConfigurationSection)ConfigurationManager.GetSection("mySampleConfigurationSectionGroup/mySampleConfigurationSection");
自定义Net Configuration 类 - 继承ConfigurationSection            
if (myConfigSection != null)

特别注意的是configure在SectionGroup的调用路径
自定义Net Configuration 类 - 继承ConfigurationSectionConfigurationManager.GetSection("mySampleConfigurationSectionGroup/mySampleConfigurationSection")


相关文章:

  • 2022-12-23
  • 2021-07-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
  • 2022-01-11
猜你喜欢
  • 2022-02-03
  • 2021-08-19
  • 2021-12-05
  • 2021-11-14
  • 2021-05-19
  • 2022-12-23
相关资源
相似解决方案