我们可以通过用自己的 XML 配置元素来扩展标准的 ASP.NET 配置设置集,要完成这一功能,我们必须实现继承System.Configuration.ConfigurationSection
类来实现自定义配置节,在1.0中当然也可以通过IconfigurationSectionHandler 接口创建自定义配置节!这里我们主要学一下通过ConfigurationSection类来实现简单的配置处理程序.
先看一下在web.config文件中的配置情况,在这里有两个元素,第一个mysection,有两个属性user,password,第二个也有两个属性element1,和element2。配置比较简单。
<!--//////////////////////////////////////////////////////////////////////////////////////////////-->
<configSections>
<sectionGroupname="mygroup">
<sectionname="mysection"
type="ConfigSection"
allowDefinition="Everywhere"
allowLocation="true"/>
</sectionGroup>
</configSections>
<!--//////////////////////////////////////////////////////////////////////////////////////////////-->
<mygroup>
<mysectionuser="用户"password="密码">
<elementelement1="属性1"element2="属性2"></element>
</mysection>
</mygroup>
理解配置文件结构后,我们就需要用继承自System.Configuration.ConfigurationSection的基类来实现简单的配置类ConfigSection,在2.0中,我们只需要这一个类就能实现完成配置,下面请看代码:
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
///<summary>
///ConfigSection的摘要说明
///</summary>
publicclassConfigSection:ConfigurationSection
{
publicConfigSection()
{
//
//TODO:在此处添加构造函数逻辑
//
}
[ConfigurationProperty("user",DefaultValue="yanghong",IsRequired=true)]
publicstringUser
{
get{return(string)this["user"];}
set{this["user"]=value;}
}
[ConfigurationProperty("password",DefaultValue="password",IsRequired=true)]
publicstringPassWord
{
get{return(string)this["password"];}
set{this["password"]=value;}
}
[ConfigurationProperty("element")]
publicelementinfoElement
{
get{return(elementinfo)this["element"];}
set{this["element"]=value;}
}
}
publicclasselementinfo:ConfigurationElement
{
publicelementinfo(){}
[ConfigurationProperty("element1",DefaultValue="element1",IsRequired=true)]
publicstringElement1
{
get{return(string)this["element1"];}
}
[ConfigurationProperty("element2",DefaultValue="element2",IsRequired=true)]
publicstringElement2
{
get{return(string)this["element2"];}
}
}
通过下面的代码就可以获得在配置文件中设置的值了
ConfigSectionconfig=(ConfigSection)ConfigurationManager.GetSection("mygroup/mysection");
Response.Write("用户名:"+config.User.ToString()+"密码:"+config.PassWord.ToString()+"元素属性:"+config.Element.Element1.ToString()+config.Element.Element2.ToString());
分享到:
相关推荐
利用ConfigurationSection实现的自定义配置节,可用于读取或写入一系列相关的配置信息到app.config或者web.config文件中.
1、重写.Net Framework中的ConfigurationSection 2、自定义SQL、Message两种类型配置工具 3、提供自定义配置文件工程;配置文件示例。
如何使用我编写的附件XSDExtractor应用程序从.NET 2.0 ConfigurationSection子类中提取XML架构(XSD)。 本文还显示了如何使用XSD文件在Visual Studio中提供验证和Intellisense支持。
维护一个相对独立的配置节,使用时需现在<ConfigSections></ConfigSections>节点下声明。我们熟悉的<appSettings></appSettings>以及<connectionStrings></connectionStrings/>就是.NET为我们预留的一个Section。 ...
您可以使用JedisBuilder.builder()并手动构建 jedis 实例或使用JedisBuilder.fromConfig(ConfigurationSection)并使用 Bukkit Configuration API 构建它 Redis 现在,创建 JedisBuilder 实例后,您可以创建一个 ...
c#中的NameValueCollection类读取配置信息,大家可以参考下。
当您只能编写单词时,请勿编写数百个同义词或复杂的正则表达式。 默认配置 Bukkit服务器的默认配置包含英语,波兰... 例如,如果您使用黑名单中的鸭子和菜鸟(如d * ck和n ** b)以及白名单中的鸭子进行配置,那么Bette
if you wan to use your custome config ...you can inherit the ConfigurationSection if your .net version is 2.0 if you want to store you config settings in another file,YOU CAN DOWNLOAD THIS DEMO PROJECT