app.config 最常用的节点介绍(一)
<!--后台数据库-->
<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
相应访问代码如下:
string Area = myNameValue["Area"];
string Device= myNameValue["Device"];
string Customer = myNameValue["Customer "];
b. DictionarySectionHandler
相应访问代码如下:
string Area = myNameValue["Area"];
string Device= myNameValue["Device"];
string Customer = myNameValue["Customer "];
c. SingleTagSectionHandler
相应访问代码如下:
string Area = myNameValue["Area"];
string Device= myNameValue["Device"];
string Customer = myNameValue["Customer "];