Net Framework Applictaion Configuration Note
在应用程序开发过程都要遇到编写XML的配置文件。一般在windows应用程序下面有个app.config XML文件,在Web Application 下面有个web.config已经机器的配置文件machine.config
1. 配置文件的基本内容,包括configuation root,然后是configuration sections
3. 自定义配置文件处理方式,定义一个类实现IConfigurationSectionHandler 或者使用ConfigurationSection
具体的见MSDN
How to: Create Custom Configuration Sections Using IConfigurationSectionHandler
How to: Create Custom Configuration Sections Using ConfigurationSection
1. 配置文件的基本内容,包括configuation root,然后是configuration sections
<configuration>
<configSections>
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source">
<sources>
<add name="File Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
filePath="path\to\external.config" />
</sources>
</enterpriseLibrary.ConfigurationSource>
</configuration>
2. 在程序中编程获得configuration setting<configSections>
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<enterpriseLibrary.ConfigurationSource selectedSource="File Configuration Source">
<sources>
<add name="File Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
filePath="path\to\external.config" />
</sources>
</enterpriseLibrary.ConfigurationSource>
</configuration>
using System.Configuration;
string config = ConfigurationManager.AppSettings["config data"];
string config = ConfigurationManager.AppSettings["config data"];
3. 自定义配置文件处理方式,定义一个类实现IConfigurationSectionHandler 或者使用ConfigurationSection
具体的见MSDN
How to: Create Custom Configuration Sections Using IConfigurationSectionHandler
How to: Create Custom Configuration Sections Using ConfigurationSection