您的位置:首页 > 移动开发

模仿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的集合属性


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"]
这么简单,源码也懒得打包了,看不懂的话就还要继续努力啊
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐