扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:中国IT实验室 来源:中国IT实验室 2007年10月2日
关键字: 编程
在本页阅读全文(共2页)
<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在定义配置文件的内容形式上是一样的,都是用
另外,如果不关心Handler类的版本等信息,可以直接省略。如NameValueSectionHandler可以直接如下声明:
<section name="myDictionary" type="System.Configuration.NameValueSectionHandler, System" />
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。