模仿AppSettings进行web.config的自定义节读取[简单实用型]
2007-10-28 00:50
423 查看
作者:垃圾猪
许多人把读自义节做的很繁,看得都累死了,刚好自己的应用程序也要用自定义节,就模仿着System.Configuration.ConfigurationSettings.AppSettings做了一个读取web.config自定义配置节的类
1.实现IConfigurationSectionHandler接口的public Object Create(Object parent, object configContext, XmlNode section)方法
2.增加NameValueCollection的集合属性
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System;
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Xml;
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Configuration;
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Collections.Specialized;
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace eWebapp
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cnblogs.com/Images/dot.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
/// 实现对web.config自定义配置节的读取
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
/// 垃圾猪 2005-11-09
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// </summary>
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class AppConfig: IConfigurationSectionHandler
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cnblogs.com/Images/dot.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Object Create(Object parent, object configContext, XmlNode section)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cnblogs.com/Images/dot.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
NameValueCollection settings;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
NameValueSectionHandler baseHandler = new NameValueSectionHandler();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
settings = (NameValueCollection)baseHandler.Create(parent, configContext, section);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return settings;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static NameValueCollection Settings
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cnblogs.com/Images/dot.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
get
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cnblogs.com/Images/dot.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
NameValueCollection
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
collection1 = (NameValueCollection) ConfigurationSettings.GetConfig("AppConfig");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return collection1;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
第二步在web.config中设置自定义节
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<configSections>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<section name="AppConfig" type="eWebapp.AppConfig, eWebapp" />
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</configSections>
第三步进行测试:
增加节点:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<AppConfig>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<add key="testString" value="自定义节读取成功了?" />
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</AppConfig>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
第四步读取:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
eWebapp.AppConfig.Settings["testString"]
这么简单,源码也懒得打包了,看不懂的话就还要继续努力啊
许多人把读自义节做的很繁,看得都累死了,刚好自己的应用程序也要用自定义节,就模仿着System.Configuration.ConfigurationSettings.AppSettings做了一个读取web.config自定义配置节的类
1.实现IConfigurationSectionHandler接口的public Object Create(Object parent, object configContext, XmlNode section)方法
2.增加NameValueCollection的集合属性
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System;
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Xml;
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Configuration;
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Collections.Specialized;
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace eWebapp
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cnblogs.com/Images/dot.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
/// 实现对web.config自定义配置节的读取
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
/// 垃圾猪 2005-11-09
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// </summary>
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class AppConfig: IConfigurationSectionHandler
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cnblogs.com/Images/dot.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Object Create(Object parent, object configContext, XmlNode section)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cnblogs.com/Images/dot.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
NameValueCollection settings;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
NameValueSectionHandler baseHandler = new NameValueSectionHandler();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
settings = (NameValueCollection)baseHandler.Create(parent, configContext, section);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return settings;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static NameValueCollection Settings
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cnblogs.com/Images/dot.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
get
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cnblogs.com/Images/dot.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
NameValueCollection
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
collection1 = (NameValueCollection) ConfigurationSettings.GetConfig("AppConfig");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return collection1;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
第二步在web.config中设置自定义节
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<configSections>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<section name="AppConfig" type="eWebapp.AppConfig, eWebapp" />
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</configSections>
第三步进行测试:
增加节点:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<AppConfig>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<add key="testString" value="自定义节读取成功了?" />
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</AppConfig>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
第四步读取:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
eWebapp.AppConfig.Settings["testString"]
这么简单,源码也懒得打包了,看不懂的话就还要继续努力啊
相关文章推荐
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- 对web.config进行新增修改删除读取操作
- 对web.config进行新增修改删除读取操作
- 对web.config进行新增修改删除读取操作
- 读取web.config中appSettings的key值
- 对web.config进行新增修改删除读取操作
- 对web.config进行新增修改删除读取操作
- [转]对web.config进行新增修改删除读取操作
- 对web.config进行新增修改删除读取操作
- .NET工程读取web.config或app.config中的appsettings时的注意事项
- Web.config中appSettings节点值两种读取方法
- 从web.config中读取AppSettings和connectionStrings信息
- 对web.config进行新增修改删除读取操作
- Web.config中appSettings节点值两种读取方法
- 使用外联文件对Web.config的appSettings进行配置
- 利用xml轻松读取web.config中的用户自定义节