自定义应用程序配置全攻略
相信大家在日常的开发工作中遇到过这样那样的信息需要通过配置文件来保存,避免对该信息的修改导致程序的重新编译,而且现在越来越多的系统开始大量使用配置文件的方式部署应用。
我这里就简单地把.net下日常用的配置方式例举一下,如有不全,还望指出!
归纳起来.net下常用的自定义配置有4种方式,如下配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="Test1" type="System.Configuration.SingleTagSectionHandler"/>
<section name="Test2" type="System.Configuration.DictionarySectionHandler"/>
<section name="Test3" type="System.Configuration.NameValueSectionHandler" />
<section name="Test4" type="MyNameSpace.CustomerSectionHandler" />
</configSections>
<Test1 setting1="Hello" setting2="World"/>
<Test2>
<add key="Hello" value="World" />
</Test2>
<Test3>
<add key="Hello" value="World" />
</Test3>
<Test4 type="Test.Config.Person,Test.Config">
<Name>james</Name>
<Age></Age>
<Other></Other>
</Test4>
</configuration>

对于自定义配置节的使用,我们会首先在<configSections>节点下定义它,然后就可以在</configuration>节点下使用了,其中每一个自定义的配置节需要一个<section>,name属性是配置节的名称,type是处理该配置节的处理器,负责解析该配置节。下面就是所配置的配置节的主体信息:
Test1 具有单个节点,可以在其中增加多个属性
Test2 可以定义字典方式的配置信息,可以在它的下面增加任意多个<add key="Hello" value="World" />
Test3 可以定义多个名称--值对
Test4 则可以定义自定义信息
下面是访问这三个配置节的代码:
//访问配置节Test1
IDictionary IDTest1 = (IDictionary)ConfigurationSettings.GetConfig("Test1");
string str = (string)IDTest1["setting1"] +" "+(string)IDTest1["setting2"];
MessageBox.Show(str); //输出Hello World
//访问配置节Test1的方法2
string[] values1=new string[IDTest1.Count];
IDTest1.Values.CopyTo(values1,0);
MessageBox.Show(values1[0]+" "+values1[1]); //输出Hello World
//访问配置节Test2
IDictionary IDTest2 = (IDictionary)ConfigurationSettings.GetConfig("Test2");
string[] keys=new string[IDTest2.Keys.Count];
string[] values=new string[IDTest2.Keys.Count];
IDTest2.Keys.CopyTo(keys,0);
IDTest2.Values.CopyTo(values,0);
MessageBox.Show(keys[0]+" "+values[0]);
//访问配置节Test3
NameValueCollection nc=(NameValueCollection)ConfigurationSettings.GetConfig("Test3");
MessageBox.Show(nc.AllKeys[0].ToString()+" "+nc["Hello"]); //输出Hello World//访问节点test4 , Person是一个自定义类型
Person per = (Person)ConfigurationSettings.GetConfig("Test4");
MessageBox.Show("Name:"+per.Name+"\r\nAge:"+per.Age);

4个获取配置的方法都用到了ConfigurationSettings.GetConfig(string str),但是他们的返回却是不同的,这是为什么呢?其实每个GetConfig方法的调用都会去使用配置文件中配置的Handler来解析相应的配置节,
|
配置节处理程序 |
返回类型 |
|
SingleTagSectionHandler |
Systems.Collections.IDictionary |
|
DictionarySectionHandler |
Systems.Collections.IDictionary |
|
NameValueSectionHandler |
Systems.Collections.Specialized.NameValueCollection |
| CustomerSectionHandler | Person |
using System;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Serialization;
using System.Configuration;
namespace Newegg.USPS.ConfigSectionHandler
{
/// <summary>
/// This class contains only one method, Create().
/// This is designed to handle custom sections in an Applications Configuration file.
/// By Implementing <see cref="IConfigurationSectionHandler"/>, we can implement the
/// Create method, which will provide the XmlNode from the configuration file. This is
/// Deserialized into an object and passed back to the Caller.
/// </summary>
/// <example>
/// Here is a configuration file entry in the <c>configSections</c> sectikon of the <c>App.Config</c>
/// file.
///<code> ///
///<section name="ServerConfig" type="ConfigSectionHandler.ConfigSectionHandler, ConfigSectionHandler" />
///</code>
///This tells the CLR that there is a section further in, with a node name of <c>ServerConfig</c>. When this section
///is to be parsed, an object of type <c>ConfigSectionHandler.ConfigSectionHandler</c> which resides in the
///assembly <c>ConfigSectionHandler</c> will be instantiated. The CLR automatically calls a method in that object
///called <c>Create</c>
///</example>
public class ConfigSectionHandler : IConfigurationSectionHandler
{
public ConfigSectionHandler() : base()
{
}
IConfigurationSectionHandler Members
}
}
该接口只有一个方法Create,该方法将配置节中的内容反序列化到指定的类中,这里是Person类,所以有了这个接口,我们可以定义任何的自定义类型到配置文件中,很方便的。
当然,配置节组是使用<sectionGroup>元素,将类似的配置节分到同一个组中。配置节组声明部分将创建配置节的包含元素,在<configSections>元素中声明配置节组,并将属于该组的节置于<sectionGroup>元素中。下面是一个包含配置节组的配置文件的例子:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="TestGroup">
<section name="Test" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<TestGroup>
<Test>
<add key="Hello" value="World"/>
</Test>
</TestGroup>
</configuration>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="TestGroup">
<section name="Test" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<TestGroup>
<Test>
<add key="Hello" value="World"/>
</Test>
</TestGroup>
</configuration>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="TestGroup">
<section name="Test" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<TestGroup>
<Test>
<add key="Hello" value="World"/>
</Test>
</TestGroup>
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="connectionstring" value="User ID=sa;Data Source=.;Password=;Initial Catalog=test;Provider=SQLOLEDB.1;" />
<add key="TemplatePATH" value="Template" />
</appSettings>
</configuration>




浙公网安备 33010602011771号