由于在每个系统中都会带有这两个配置文件中的一个,所以把一些需要的配置信息放在这里里面是非常有效的,既不需要产生新的自定义配置文件,又能达到效果。
实现方式:
C#
有
Configuration
类来读取
config
中的节点,但是只提供直接读取
AppSetting
的方法(这个是特殊方法),其它的节点只能通过
GetConfig()
来获取,具体方式如下:
1、
配置
App.config
文件
<configSections>
<sectionGroup name="sectionGroup1">
<section name="Section1" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
这一部分主要是来定义即将使用的
Section
节点名称,
section
的
type
包括以下
2
种:
a.
DictionarySectionHandler
b.
NameValueSectionHandler
c.
指定
type
的作用是指定从配置文件中读取节的配置节处理程序类的名称。
Sectiongroup
定义一个配置节的命名空间,
section
元素则是配置节声明。
2、
添加如下的自定义节点:
<sectionGroup1>
<Section1>
<add key="param1" value = "7"/>
</Section1>
</sectionGroup1>
3、
读取方式:
NameValueCollection config = (NameValueCollection)ConfigurationSettings.GetConfig("sectionGroup1/Section1");
该函数必须包含于System.Configuration 名称空间下。
4、
注意事项:
a.
appSettings
节点必须放在自定义节点后,这个具体原因还不知道。
b.
sectiongroup
可以相互嵌套,但必须保证唯一性