标记中。如:
<xml version="1.0" encoding="utf-8"?>
<appSettings>
<add key="LogFile" value="d:\log\debug.log"/>
appSettings>
< SPAN>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"/>
< SPAN>myDictionary>
<myNameValue>
<add key="Area" value="Fuzhou"/>
<add key="Device" value="Printer"/>
<add key="Customer" value="Muf"/>
< SPAN>myNameValue>
<myInfo
Area="Fuzhou" Device="Printer" Customer="Muf"
/>
< SPAN>myConfig>
< SPAN>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" />
< SPAN>sectionGroup>
< SPAN>configSections>
声明和配置的关系,示意图如下:
由图上可以看出,NameValueSectionHandler和DictionarySectionHandler在定义配置文件的内容形式上是一样的,都是用来设置内容的。只是返回到C#中的类不太一样,可以参考下面的代码示例。
另外,如果不关心Handler类的版本等信息,可以直接省略。如NameValueSectionHandler可以直接如下声明:
<section name="myDictionary" type="System.Configuration.NameValueSectionHandler, System" />
查看本文来源