app.config 最常用的节点介绍(一)

 在程序中,经常碰到需要对程序的数据做一些准备。初始化 和程序选项选择的数据,我们都可以把这些数据保存在程序中,一是可以看得清楚,二是可以再程序开发完成后,过一些改动。
    app.config 是一个标准的xml文件(区分大小写)。可以右击想添加类一样添加,但添加的不是类是应用程序配置文件
 
  
添加完后 就可以看到了 下面说一下它的结构
1 有一个根节点是configuration 在这个节点力我们可以建立不同的配置项
最常见的有
appsetting 
appSettings 配置节为整个程序的配置,如果是对当前用户的配置,请使用 userSettings 配置节,其格式与以下配置书写要求一样。
 
配置类似于键值对这样的数据
 
connectionstring 节点
  <connectionStrings>

<!--后台数据库-->
<add name="db" connectionString="Persist Security Info=False;Data Source=wufuqimingsi;Initial Catalog=SCM;User ID=123;Password=1234" providerName="System.Data.SqlClient" />

  </connectionStrings>

自定义节点

<!--自定义节点>

<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>  

sectionGroup 元素充当 section 元素的容器。

section 元素将配置节处理程序与配置元素或节关联。由于 ASP.NET 不对如何处理配置文件内的设置作任何假设,因此这非常必要。但 ASP.NET 会将配置数据的处理委托给配置节处理程序,(稍候说明。)每个 section 元素均标识一个配置节或元素。可以在 sectionGroup 元素中对 section 元素进行逻辑分组,以对 section 元素进行组织并避免命名冲突。section 和 sectionGroup 元素包含在 configSections 元素中。

sectionGroup节点属性:

name:必选的 String 属性,这是 group 元素在配置文件的节设置区域中使用的名称。

section节点属性:

name:必选的 String 属性,指定与 type 属性中指定的配置节处理程序关联的配置节或元素的名称。这是该元素在配置文件的节设置区域中使用的名称。

type:必选的 String 属性,指定用来执行如下操作的配置节处理程序类的名称:处理在 name 属性中指定的节或元素中的配置设置。

type 主要系统提供了三种类型。但可以自己实现接口定义自己需要的

  .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 "];
 
注意 访问自定义节点和系统的定义的节点方式是不一样的 。在下一篇中会有讲 配置文件的读写。
 就先写着3中节点吧 。好像我用的就这些了。其他没有用过 大家可以对其他节点的用法交流
 
posted @ 2013-07-26 10:08  x7hace  阅读(87)  评论(0编辑  收藏  举报