沐枫小筑

script c c++ c++/cli c#
.net windows ria game and so ...

  博客园 :: 首页 :: 联系 :: 订阅 订阅 :: 管理
  52 Posts :: 1 Stories :: 379 Comments :: 6 Trackbacks
    .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 沐枫 阅读(6084) 评论(17)  编辑 收藏 所属分类: .NET

Feedback

#1楼  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>

-----------------------------------------------------------------------------------------
关于上面这一部分能不能再解释一下?谢谢!
  回复  引用  查看    

#2楼 [楼主] 2005-09-07 10:14 沐枫      
YuL: 已经在上文增加图片说明了
  回复  引用  查看    

#3楼  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");
可也读不出任何数据来。请楼主解释下
  回复  引用  查看    

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

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

我已经改在上文改掉了。

  回复  引用  查看    

#5楼  2005-12-01 20:37 Shdow [未注册用户]
请教一下:NET已经定义了3种配置类型。这三种类型有什么特别的区别吗?怎么判断该使用哪种呢?
  回复  引用    

#6楼  2005-12-02 15:29 大尾巴狼      
ConfigurationSettings.GetConfig(@"myConfig\myInfo");
应该
ConfigurationSettings.GetConfig("myConfig/myInfo");

右斜杠
  回复  引用  查看    

#7楼 [楼主] 2005-12-15 09:42 沐枫      
answer@Shdow.com

这三种,NameValueSectionHandler和DictionarySectionHandler在使用上实际是没有区别的.不知道效率上有没有区别,没有研究过.

但是SingleTagSectionHandler的区别就比较大了.
SingleTagSectionHandler在定义时,配置项都定义在属性当中了,如:
<myconfig Info="test" content="mycontent">

而NameValueSectionHandler和DictionarySectionHandler都是定义在<add/>中,如:
<myconfig>
<add key="Info" value="test"/>
<add key="content" value="mycontent"/>
</myconfig>

answer@大尾巴狼.com

两种写法都可以,主要还是看各人的习惯.

  回复  引用  查看    

#8楼  2006-01-09 16:16 合金枪头      
请教个问题:
我在配置文件中写:
type="System.Configuration.NameValueSectionHandler, System"
然后在程序中使用GetConfig读取该节,在运行时会报一个“无法创建类型System.Configuration.NameValueSectionHandler, System”的错误。然后我把配置文件中的写法改成:
type="System.Configuration.NameValueSectionHandler"
就一切正常了,读出来的东西也都对。

这是怎么回事?

  回复  引用  查看    

#9楼 [楼主] 2006-01-13 10:37 沐枫      
你所说的,我没有试验。
不过,感觉上象是和强类型有关。所以,假如你写成:
System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
应该就可以了。(当然,不同版本的System.dll,其Version、PublicKeyToken可能不一样,上例中只是.net 1.1中的System.dll)
  回复  引用  查看    

#10楼  2006-08-09 08:46 sd [未注册用户]
靠,太他妈复杂了,只是存两个单词,至于吗?
  回复  引用    

#11楼 [楼主] 2006-08-09 09:23 沐枫      
没办法,这是微软推荐的规范。
我们可以自已采用别的方法,如ini文件,或自已定义的配置格式,甚至文本文件或直接写在程序中。

也正是因为比较复杂,所以,在VS2005中,对配置文件专门有个配置界面,这样,只要填填表格,就可以了(VS2005会自动生成这个复杂的配置文件,以及相关的函数和类来访问配置内容)。
  回复  引用  查看    

#12楼  2006-10-25 16:04 yoyolion[匿名]      
ConfigurationSettings.GetConfig(@"myConfig\myInfo");
应该
ConfigurationSettings.GetConfig("myConfig/myInfo");


第一种写法是不行的!!!楼主你有试过第一种写法吗?
msdn中是第二种写法
  回复  引用  查看    

#13楼 [楼主] 2006-10-26 13:45 沐枫      
@大尾巴狼
@yoyolion[匿名]
谢谢两位。

  回复  引用  查看    


也正是因为比较复杂,所以,在VS2005中,对配置文件专门有个配置界面,这样,只要填填表格,就可以了(VS2005会自动生成这个复杂的配置文件,以及相关的函数和类来访问配置内容)。
----------------------------------
怎么打开??谢谢
  回复  引用    

#15楼  2007-08-22 14:28 BrianLei      
@沐枫
@合金枪头
和“合金枪头 ”遇到相同的问题
请教个问题:
我在配置文件中写:
type="System.Configuration.NameValueSectionHandler, System"
然后在程序中使用GetConfig读取该节,在运行时会报一个“无法创建类型System.Configuration.NameValueSectionHandler, System”的错误。然后我把配置文件中的写法改成:
type="System.Configuration.NameValueSectionHandler"
就一切正常了,读出来的东西也都对。

这是怎么回事?
  回复  引用  查看    

#16楼 [楼主] 2007-08-23 13:54 沐枫      
单写system,可能会出错。因为.net要去找符合要求的system.dll或system.exe,而此时信息不够全。比如少了强类型说明。如在.net1.1中应为:System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

而不写就可以,是因为,你的程序中已经把system.dll加载引用了,因此,可能.net就可以直接引用你程序中引用的那个system.dll。所以可以成功。
  回复  引用  查看    


标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2006-10-26 13:43 编辑过