在Web.config或App.config中的添加自定义配置
.Net中的System.Configuration命名空间为我们在web.config或者app.config中自定义配置提供了完美的支持。最近看到一些项目中还在自定义xml文件做程序的配置,所以忍不住写一篇用系统自定义配置的随笔了。
如果你已经对自定义配置了如指掌,请忽略这篇文章。
言归正传,我们先来看一个最简单的自定义配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="simple" type="ConfigExample.Configuration.SimpleSection,ConfigExample"/>
</configSections>
<simple maxValue="20" minValue="1"></simple>
</configuration>
在配置文件中使用自定义配置,需要在configSections中添加一个section元素,并制定此section元素对应的类型和名字。然后再在configuration根节点下面添加此自定义配置,如上例中的simple节点。simple节点只有两个整形数的属性maxValue和minValue。
要在程序中使用自定义配置我们还需要实现存取这个配置块的类型,一般需要做如下三件事:
1. 定义类型从System.Configuration.ConfigurationSection继承
2. 定义配置类的属性,这些属性需要用ConfigurationProperty特性修饰,并制定属性在配置节中的名称和其他一些限制信息
3. 通过基类的string索引器实现属性的get ,set
非常简单和自然,如下是上面配置类的实现:
public class SimpleSection:System.Configuration.ConfigurationSection
{
[ConfigurationProperty("maxValue",IsRequired=false,DefaultValue=Int32.MaxValue)]
public int MaxValue
{
get
{
return (int)base["maxValue"];
}
set
{
base["maxValue"] = value;
}
} [ConfigurationProperty("minValue",IsRequired=false,DefaultValue=)]
public int MinValue
{
get { return (int) base["minValue"];}
set { base["minValue"] = value; }
} [ConfigurationProperty("enabled",IsRequired=false,DefaultValue=true)]
public bool Enable
{
get
{
return (bool)base["enabled"];
}
set
{
base["enabled"] = value;
}
}
}
这样子一个简单的配置类就完成了,怎么在程序中使用这个配置呢?需要使用ConfigurationManager类(要引用System.configuration.dll这个dll只有在.Net2.0之后的版本中才有)的GetSection方法获得配置就可以了。如下代码:
SimpleSection simple = ConfigurationManager.GetSection("simple") as SimpleSection;
Console.WriteLine("simple minValue={0} maxValue = {1}",simple.MinValue,simple.MaxValue);
这个配置类太过简陋了,可能有时候我们还需要更复杂的构造,比如在配置类中使用类表示一组数据,下面我们看一个稍微复杂一点的自定义配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="complex" type="ConfigExample.Configuration.ComplexSection,ConfigExample"/>
</configSections>
<complex height="190">
<child firstName="James" lastName="Bond"/>
</complex>
</configuration>
这个配置的名字是complex,他有一个属性height,他的节点内还有一个child元素这个元素有两个属性firstName和lastName;对于这个内嵌的节点该如何实现呢?首先我们需要定义一个类,要从ConfigurationElement类继承,然后再用和SimpleSection类似的方法定义一些用ConfigurationProperty特性修饰的属性就可以了,当然属性值的get,set也要使用基类的索引器。如下实现:
public class ComplexSection : ConfigurationSection
{
[ConfigurationProperty("height", IsRequired = true)]
public int Height
{
get
{
return (int)base["height"];
}
set
{
base["height"] = value;
}
} [ConfigurationProperty("child", IsDefaultCollection = false)]
public ChildSection Child
{
get
{
return (ChildSection)base["child"];
}
set
{
base["child"] = value;
}
}
} public class ChildSection : ConfigurationElement
{
[ConfigurationProperty("firstName", IsRequired = true, IsKey = true)]
public string FirstName
{
get
{
return (string)base["firstName"];
}
set
{
base["firstName"] = value;
}
} [ConfigurationProperty("lastName", IsRequired = true)]
public string LastName
{
get
{
return (string)base["lastName"];
}
set
{
base["lastName"] = value;
}
}
}
还有稍微再复杂一点的情况,我们可能要在配置中配置一组相同类型的节点,也就是一组节点的集合。如下面的配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="complex" type="ConfigExample.Configuration.ComplexSection,ConfigExample"/>
</configSections>
<complex height="190">
<child firstName="James" lastName="Bond"/> <children>
<add firstName="Zhao" lastName="yukai"/>
<add firstName="Lee" lastName="yukai"/>
<remove firstName="Zhao"/>
</children>
</complex>
</configuration>
请看children节点,它就是一个集合类,在它里面定义了一组add元素,也可以有remove节点把已经添进去的配置去掉。
要使用自定义节点集合需要从ConfigurationElementCollection类继承一个自定义类,然后要实现此类GetElementKey(ConfigurationElement element)和ConfigurationElement CreateNewElement()两个方法;为了方便的访问子节点可以在这个类里面定义只读的索引器。请看下面的实现
public class Children : ConfigurationElementCollection
{
protected override object GetElementKey(ConfigurationElement element)
{
return ((ChildSection)element).FirstName;
} protected override ConfigurationElement CreateNewElement()
{
return new ChildSection();
} public ChildSection this[int i]
{
get
{
return (ChildSection)base.BaseGet(i);
}
} public ChildSection this[string key]
{
get
{
return (ChildSection)base.BaseGet(key);
}
} }
当然要使用此集合类我们必须在Complex类中添加一个此集合类的属性,并要指定集合类的元素类型等属性,如下:
[ConfigurationProperty("children", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(ChildSection), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap, RemoveItemName = "remove")]
public Children Children
{
get
{
return (Children)base["children"];
}
set
{
base["children"] = value;
}
}
我们会经常用到类似appSettings配置节的键值对的构造,这时候我们就不必再自己实现了,我们可以直接使用现有的System.Configuration.NameValueConfigurationCollection类来定义一个自定义的键值对。可以在Complex类中定义如下属性
[ConfigurationProperty("NVs", IsDefaultCollection = false)]
public System.Configuration.NameValueConfigurationCollection NVs
{
get
{
return (NameValueConfigurationCollection)base["NVs"];
}
set
{
base["NVs"] = value;
}
}
然后在配置文件的complex节中添加键值对配置
<NVs>
<add name="abc" value="123"/>
<add name="abcd" value="12d3"/>
</NVs>
到这儿已经基本上可以满足所有的配置需求了。不过还有一点更大但是不复杂的概念,就是sectionGroup。我们可以自定义SectionGroup,然后在sectionGroup中配置多个section;分组对于大的应用程序是很有意义的。
如下配置,配置了一个包含simple和一个complex两个section的sectionGroup
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup type="ConfigExample.Configuration.SampleSectionGroup,ConfigExample" name="sampleGroup">
<section type="ConfigExample.Configuration.SimpleSection,ConfigExample" allowDefinition="Everywhere" name="simple" />
<section type="ConfigExample.Configuration.ComplexSection,ConfigExample" allowDefinition="Everywhere" name="complex"/>
</sectionGroup>
</configSections>
<sampleGroup>
<simple maxValue="20" minValue="1">
</simple> <complex height="190">
<child firstName="James" lastName="Bond"/>
<children>
<add firstName="Zhao" lastName="yukai"/>
<add firstName="Lee" lastName="yukai"/>
<remove firstName="Zhao"/>
</children>
<NVs>
<add name="abc" value="123"/>
<add name="abcd" value="12d3"/>
</NVs>
</complex>
</sampleGroup>
</configuration>
为了方便的存取sectionGroup中的section我们可以实现一个继承自System.Configuration.ConfigurationSectionGroup类的自定义类。实现很简单,就是通过基类的Sections[“sectionName”]索引器返回Section。如下:
public class SampleSectionGroup : System.Configuration.ConfigurationSectionGroup
{
public SimpleSection Simple
{
get
{
return (SimpleSection)base.Sections["simple"];
}
} public ComplexSection Complex
{
get
{
return (ComplexSection)base.Sections["complex"];
}
}
}
在Web.config或App.config中的添加自定义配置的更多相关文章
- 修改和获取web.config或app.config文件appSettings配置节中的Add里的value属性 函数
1: /// <summary> 2: /// 修改web.config或app.config文件appSettings配置节中的Add里的value属性 3: /// </summ ...
- 在Web.config或App.config中的添加自定义配置 <转>
.Net中的System.Configuration命名空间为我们在web.config或者app.config中自定义配置提供了完美的支持.最近看到一些项目中还在自定义xml文件做程序的配置 ...
- 说说Web.Config与App.Config
说到web.config和app.config大家都很熟悉,我们都叫他们配置文件,平时用的多,注意的少.两个有啥区别呢,很简单,一句话:如果是web程序,如webform项目类型和mvc项目类型就是w ...
- 一个web.Config或app.Config自定义段configSections的示例
一个web.Config或app.Config自定义段configSections的示例 越来越觉得,直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml ...
- 一个web.Config或app.Config自定义段configSections的示例--转
直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml配置文件,简洁方便得多.这两个配置文件不仅有常见的connectionStrings和appSetti ...
- .NET下对Web.config与App.Config的增删改操作的代码
把代码过程常用的内容做个收藏,下边代码段是关于 .NET下对Web.config与App.Config的增删改操作的代码. <?xml version="1.0" encod ...
- .net分布在指定文件夹的web.confgi或者app.config
.Net里面,ConfigurationManager默认读取的是Web.config或者App.config但是,什么都放在这两个文件里面,感觉太多了,也不好管理配置.于是参考了下别人的资料,自己写 ...
- web.config or app.config 中configSections配置节点
以前还真没见过,今天看项目中有在用,简单写了个Demo,这样配置的好处就是可以自定义配置,更加模块化,直接上代码; 1.配置文件 由于我创建的是一个控制台项目,所以配置文件是App.Config:(这 ...
- 配置文件(Machine.config、Web.config、App.config)
Machine.config1.该文件在Windows目录下\Microsoft.net\framework\[version]\Config\2.为了提高性能,该文件只包含不同于默认值的设置.并且定 ...
随机推荐
- nodejs学习笔记
这里有一段废话,我写了又删,删了又写,反复了十来次,最后还是把它删除了.不用想了,那是一段牢骚话兼有煽情意思. 好了,还是把我学习nodejs的笔记记录下来吧,一来复习巩固,加深印象,二来自己实践中出 ...
- JavaScript encodeURI(), decodeURI(), encodeURIComponent(), decodeURIComponent()
URI: Uniform Resource Identifier encodeURI() And decodeURI() The encodeURI() function is used to en ...
- osip状态机分析
转载于:http://blog.csdn.net/lbc2100/article/details/48342889 OSIP的核心是系统状态机,在不同情况下,系统处于不同的状态,在某一状态下当系统发生 ...
- 改变Web Browser控件IE版本
默认的webbrowser控件使用的渲染模式版本似乎是IE7,想要更改更高版本,如下: 在注册表位置 HKEY_CURRENT_USER\Software\Microsoft\Internet Exp ...
- 2015年江西理工大学C语言程序设计竞赛(高级组)
A 解法:DP+二分 dp[i]=max(dp[i],dp[j]+p[i].v)(i>j) dp[i]表示建立i点之后能够获得的最大值 int n,M; struct node { int l, ...
- NSISの堆栈操作
一 .堆栈 堆栈是 NSIS 维护的一堆数据,你可以根据需要往堆栈中存入任意大小的数据(as big as you like),所以你可以向堆栈中推入或读取数据,堆栈只有一个,堆栈遵守 LIFO (后 ...
- kibana安装与基础用法
来自官网,版本为4.5 下载rpm包并安装 wget -c https://download.elastic.co/kibana/kibana/kibana-4.5.4-1.x86_64.rpm rp ...
- 不用安装Oracle_Client就能使用PLSQL_Developer
1. 下载oracle的客户端程序包(30M) 只需要在Oracle官方网站下载一个叫Instant Client Package的软件就可以了,这个软件不需要安装,只要解压就可以用了,很 ...
- RPM安装rabbitMQ
系统使用的是centos 7 - minimal 建立用户和组: # groupadd rabbitmq # useradd rabbitmq -g rabbitmq 在安装rabbitMQ之前需要先 ...
- 手机测试pc端网页
在这个问题上徘徊了 一个钟头了,终于被我找到方法了,就赶紧记下来,以后好查阅!! 主要问题在防火墙,防火墙阻当了80端口,所以怎么用手机访问都是访问不了的.把防火墙关闭就好了! 贴上httpd-vho ...