作者:垃圾猪
许多人把读自义节做的很繁,看得都累死了,刚好自己的应用程序也要用自定义节,就模仿着System.Configuration.ConfigurationSettings.AppSettings做了一个读取web.config自定义配置节的类
1.实现IConfigurationSectionHandler接口的public Object Create(Object parent, object configContext, XmlNode section)方法
2.增加NameValueCollection的集合属性
using System;
using System.Xml;
using System.Configuration;
using System.Collections.Specialized;
![]()
namespace eWebapp
{
/// <summary>
/// 实现对web.config自定义配置节的读取
/// 垃圾猪 2005-11-09
/// </summary>
public class AppConfig: IConfigurationSectionHandler
{
![]()
public Object Create(Object parent, object configContext, XmlNode section)
{
NameValueCollection settings;
![]()
NameValueSectionHandler baseHandler = new NameValueSectionHandler();
![]()
settings = (NameValueCollection)baseHandler.Create(parent, configContext, section);
![]()
return settings;
}
public static NameValueCollection Settings
{
get
{
NameValueCollection
collection1 = (NameValueCollection) ConfigurationSettings.GetConfig("AppConfig");
return collection1;
}
}
![]()
}
![]()
}
第二步在web.config中设置自定义节
<configSections>
<section name="AppConfig" type="eWebapp.AppConfig, eWebapp" />
</configSections>
第三步进行测试:
增加节点:
<AppConfig>
<add key="testString" value="自定义节读取成功了?" />
</AppConfig>
第四步读取:
eWebapp.AppConfig.Settings["testString"]
许多人把读自义节做的很繁,看得都累死了,刚好自己的应用程序也要用自定义节,就模仿着System.Configuration.ConfigurationSettings.AppSettings做了一个读取web.config自定义配置节的类
1.实现IConfigurationSectionHandler接口的public Object Create(Object parent, object configContext, XmlNode section)方法
2.增加NameValueCollection的集合属性
using System;
using System.Xml;
using System.Configuration;
using System.Collections.Specialized;
namespace eWebapp
{
/// <summary>
/// 实现对web.config自定义配置节的读取
/// 垃圾猪 2005-11-09
/// </summary>
public class AppConfig: IConfigurationSectionHandler
{
public Object Create(Object parent, object configContext, XmlNode section)
{
NameValueCollection settings;
NameValueSectionHandler baseHandler = new NameValueSectionHandler();
settings = (NameValueCollection)baseHandler.Create(parent, configContext, section);
return settings;
}
public static NameValueCollection Settings
{
get
{
NameValueCollection
collection1 = (NameValueCollection) ConfigurationSettings.GetConfig("AppConfig");
return collection1;
}
}
}
}
<configSections>
<section name="AppConfig" type="eWebapp.AppConfig, eWebapp" />
</configSections> 第三步进行测试:
增加节点:
<AppConfig>
<add key="testString" value="自定义节读取成功了?" />
</AppConfig> 
eWebapp.AppConfig.Settings["testString"]这么简单,源码也懒得打包了,看不懂的话就还要继续努力啊


浙公网安备 33010602011771号