.NET的应用程序配置文件,使用的是XML格式。相对INI文件来说,它的功能要强上不少,而且具有很强的可扩展性。它的缺点是不能直接进行写操作,也就是说,不能直接在程序中修改配置文件的数据(当然不是指不能,不过不是本文讨论的范围)。本文主要目的是探讨如何扩展配置文件,并在其加入各种自定义配置信息。
.NET的应用程序配置文件,使用的是XML格式。相对INI文件来说,它的功能要强上不少,而且具有很强的可扩展性。它的缺点是不能直接进行写操作,也就是说,不能直接在程序中修改配置文件的数据(当然不是指不能,不过不是本文讨论的范围)。本文主要目的是探讨如何扩展配置文件,并在其加入各种自定义配置信息。
1. 使用
1<appsettings>
2简单的配置信息,可以直接放入<appsettings>标记中。如:
3<?xml version="1.0" encoding="utf-8"?>
4<appsettings>
5<add key="LogFile" value="d:\log\debug.log"></add>
6</appsettings>
7
8相应访问代码如下:
9string fileName = System.Configuration.ConfigurationSettings.AppSettings.Get("LogFile");
10
112\. 自定义配置节(section)名称
12比如,我们要使用下面的配置结构,将配置信息归类分组:
13<?xml version="1.0" encoding="utf-8"?>
14<configuration>
15<!-- 需要在此处加入自定义配置声明 -->
16<!-- 以下是自定义配置的内容 -->
17<myconfig>
18<mydictionary>
19<add key="Area" value="Fuzhou"></add>
20<add key="Device" value="Printer"></add>
21<add key="Customer" value="Muf"></add>
22</mydictionary>
23<mynamevalue>
24<add key="Area" value="Fuzhou"></add>
25<add key="Device" value="Printer"></add>
26<add key="Customer" value="Muf"></add>
27</mynamevalue>
28<myinfo area="Fuzhou" customer="Muf" device="Printer"></myinfo>
29</myconfig>
30</configuration>
31
32但是这样是不行的。我们必须要在配置文件前面加入声明:
33<!-- 以下是自定义配置的声明 -->
34<configsections>
35<sectiongroup name="myConfig">
36<section name="myDictionary" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"></section>
37<section name="myNameValue" type="System.Configuration.DictionarySectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"></section>
38<section name="myInfo" type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"></section>
39</sectiongroup>
40</configsections>
41
42把这一段放入配置文件中,我们的配置结构就可以正常使用了。声明中,<sectiongroup>用来定义不含配置数据的节的名称。<section>用来定义含有自定义配置数据的节的名称。<section type="">用来指定定义配置数据的类型。.NET已经定义了3种配置类型:
43a. NameValueSectionHandler
44相应访问代码如下:
45NameValueCollection myNameValue= (NameValueCollection)System.Configuration.ConfigurationSettings.AppSettings.Get(@"myConfig\myNameValue");
46string Area = myNameValue["Area"];
47string Device= myNameValue["Device"];
48string Customer = myNameValue["Customer "];
49
50b. DictionarySectionHandler
51相应访问代码如下:
52Hashtable myNameValue= (Hashtable)System.Configuration.ConfigurationSettings.AppSettings.Get(@"myConfig\myDictionary");
53string Area = myNameValue["Area"];
54string Device= myNameValue["Device"];
55string Customer = myNameValue["Customer "];
56
57c. SingleTagSectionHandler
58相应访问代码如下:
59Hashtable myNameValue= (Hashtable)System.Configuration.ConfigurationSettings.AppSettings.Get(@"myConfig\myInfo");
60string Area = myNameValue["Area"];
61string Device= myNameValue["Device"];
62string Customer = myNameValue["Customer "];
63
64
65这三种类型的详细信息,可以参考 MSDN 文档。同时.NET 还定义了IgnoreSectionHandler类型,为 System.Configuration 之外的系统所读取和处理的配置节提供节处理程序定义。
66除此之外,.NET提供了IConfigurationSectionHandler接口,这样我们还可以自行进行扩展,以设计出我们自已的配置形式。
67
68(待续)</section></section></sectiongroup></appsettings></appsettings>