chiname

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
    .NET的应用程序配置文件,使用的是XML格式。相对INI文件来说,它的功能要强上不少,而且具有很强的可扩展性。它的缺点是不能直接进行写操作,也就是说,不能直接在程序中修改配置文件的数据(当然不是指不能,不过不是本文讨论的范围)。本文主要目的是探讨如何扩展配置文件,并在其加入各种自定义配置信息。
    
如何使用.NET配置文件(一)    如何使用.NET配置文件(二)    沐枫网志

    1. 使用<appSettings>
        简单的配置信息,可以直接放入<appSettings>标记中。如:
<?xml version="1.0" encoding="utf-8"?>
  
<appSettings>
 
<add key="LogFile" value="d:\log\debug.log"/>
  
</appSettings>  
</configuration>

        相应访问代码如下:       

string fileName = System.Configuration.ConfigurationSettings.AppSettings.Get("LogFile");

     2. 自定义配置节(section)名称
        比如,我们要使用下面的配置结构,将配置信息归类分组:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- 需要在此处加入自定义配置声明 -->
<!-- 以下是自定义配置的内容 -->
<myConfig>
  
<myDictionary>
    
<add key="Area" value="Fuzhou"/>
    
<add key="Device" value="Printer"/> 
    
<add key="Customer" value="Muf"/>
  
</myDictionary>
  
<myNameValue>
    
<add key="Area" value="Fuzhou"/>
    
<add key="Device" value="Printer"/> 
    
<add key="Customer" value="Muf"/>
  
</myNameValue>
  
<myInfo
    
Area="Fuzhou" Device="Printer" Customer="Muf"
  
/>
</myConfig>
</configuration>

        但是光这样子说明是不行的。没有声明,是不能使用自定义的配置段。我们必须要在配置文件前面加入声明:  

<!-- 以下是自定义配置的声明 -->
  
<configSections>
    
<sectionGroup name="myConfig">
         
<section name="myDictionary"
            type
="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        
<section name="myNameValue"
            type
="System.Configuration.DictionarySectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        
<section name="myInfo"
            type
="System.Configuration.SingleTagSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    
</sectionGroup>
  
</configSections>  

    声明和配置的关系,示意图如下:
        
    由图上可以看出,NameValueSectionHandler和DictionarySectionHandler在定义配置文件的内容形式上是一样的,都是用<add>来设置内容的。只是返回到C#中的类不太一样,可以参考下面的代码示例。
    另外,如果不关心Handler类的版本等信息,可以直接省略。如NameValueSectionHandler可以直接如下声明:

<section name="myDictionary"            type="System.Configuration.NameValueSectionHandler, System" />

    把上面的<configSections>声明段放入配置文件中,我们的配置结构就可以正常使用了。声明中,< sectionGroup>用来定义不含配置数据的节的名称。<section>用来定义含有自定义配置数据的节的名称。< section type>用来指定定义配置数据的类型。

    注意,自定义的配置节,不能使用 System.Configuration.ConfigurationSettings.AppSettings.Get 来访问,要使用 System.Configuration.ConfigurationSettings.GetConfig

    .NET已经定义了3种配置类型:
  a. NameValueSectionHandler
        相应访问代码如下: 

NameValueCollection myNameValue= (NameValueCollection)System.Configuration.ConfigurationSettings.GetConfig(@"myConfig\myNameValue");
string Area = myNameValue["Area"];
string Device= myNameValue["Device"];
string Customer = myNameValue["Customer "];

  b. DictionarySectionHandler
        相应访问代码如下:

Hashtable myNameValue= (Hashtable)System.Configuration.ConfigurationSettings.GetConfig(@"myConfig\myDictionary");
string Area = myNameValue["Area"];
string Device= myNameValue["Device"];
string Customer = myNameValue["Customer "];

  c. SingleTagSectionHandler
        相应访问代码如下:   

Hashtable myNameValue= (Hashtable)System.Configuration.ConfigurationSettings.GetConfig(@"myConfig\myInfo");
string Area = myNameValue["Area"];
string Device= myNameValue["Device"];
string Customer = myNameValue["Customer "];

        这三种类型的详细信息,可以参考 MSDN 文档。同时.NET 还定义了IgnoreSectionHandler类型,为 System.Configuration 之外的系统所读取和处理的配置节提供节处理程序定义。
        除此之外,.NET提供了IConfigurationSectionHandler接口,这样我们还可以自行进行扩展,以设计出我们自已的配置形式。

(待续)

posted on 2005-09-06 16:35 沐枫 阅读(1315) 评论(5)  编辑 收藏 收藏至365Key 所属分类: .NET

Feedback

# re: 如何使用.NET配置文件(一) 2005-09-07 09:29 YuL
<configSections>
<sectionGroup name="myConfig">
<section name="myDictionary"
type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="myNameValue"
type="System.Configuration.DictionarySectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="myInfo"
type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</sectionGroup>
</configSections>

-----------------------------------------------------------------------------------------
关于上面这一部分能不能再解释一下?谢谢!
  

# re: 如何使用.NET配置文件(一) 2005-09-07 10:14 沐枫
YuL: 已经在上文增加图片说明了
  

# re: 如何使用.NET配置文件(一) 2005-09-27 16:12 波仔
我按楼主的代码实验了下,编译不通过
System.Configuration.ConfigurationSettings.AppSettings.Get返回类型是
string。(NameValueCollection)System.Configuration.ConfigurationSettings.AppSettings.Get(@"myConfig\myNameValue");
显然是编不过的。
我然后将代码稍加改动
string[] myNameValue= System.Configuration.ConfigurationSettings.AppSettings.GetValues(@"myConfig\myNameValue");
可也读不出任何数据来。请楼主解释下
  

# re: 如何使用.NET配置文件(一) 2005-10-26 10:34 沐枫
@波仔
非常报歉,代码在从程序中拷出来时,我作了一些处理,避免一些与文章无关的东西带出来。
但还是出一个问题,这就是你编译不通过的主要原因:

自定义的配置节,不能使用 System.Configuration.ConfigurationSettings.AppSettings.Get 来访问,要使用 System.Configuration.ConfigurationSettings.GetConfig。

我已经改在上文改掉了。

  

# re: 如何使用.NET配置文件(一) 2005-12-01 20:37 Shdow
请教一下:NET已经定义了3种配置类型。这三种类型有什么特别的区别吗?怎么判断该使用哪种呢?
posted on 2005-12-02 09:37  把我的欢乐带给你  阅读(139)  评论(0)    收藏  举报