.NET配置文件 使用详解

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

1. 使用<appSettings>
        简单的配置信息,可以直接放入<appSettings>标记中。如:

 

ContractedBlock.gifExpandedBlockStart.gifCode
<?xml version="1.0" encoding="utf-8"?>
  
<appSettings>
 
<add key="LogFile" value="d:\log\debug.log"/>
  
</appSettings>  
</configuration>

 

  相应访问代码如下:  

 

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

 

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

 

ContractedBlock.gifExpandedBlockStart.gifCode
<?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>

 

但是光这样子说明是不行的。我们必须要在配置文件前面加入声明: 

 

ContractedBlock.gifExpandedBlockStart.gifCode
<!-- 以下是自定义配置的声明 -->
  
<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>  

 把这一段放入配置文件中,我们的配置结构就可以正常使用了。声明中,<sectionGroup>用来定义不含配置数据的节的名称。<section>用来定义含有自定义配置数据的节的名称。<section type>用来指定定义配置数据的类型。.NET已经定义了3种配置类型:
  a. NameValueSectionHandler
        相应访问代码如下: 

 

ContractedBlock.gifExpandedBlockStart.gifCode
NameValueCollection myNameValue= (NameValueCollection)System.Configuration.ConfigurationSettings.AppSettings.Get(@"myConfig\myNameValue");
string Area = myNameValue["Area"];
string Device= myNameValue["Device"];
string Customer = myNameValue["Customer "];

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

 

ContractedBlock.gifExpandedBlockStart.gifCode
Hashtable myNameValue= (Hashtable)System.Configuration.ConfigurationSettings.AppSettings.Get(@"myConfig\myDictionary");
string Area = myNameValue["Area"];
string Device= myNameValue["Device"];
string Customer = myNameValue["Customer "];

转载于:https://www.cnblogs.com/Dlonghow/archive/2008/08/04/1259651.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值