标签:
自定义节
1.首先在<configSections>中定义自定义配置节(例如Index、testSection)和对应的自定义配置节处理程序(例如NameValueSectionHandler)
2.然后添加节的内容
<configuration> <configSections> <sectionGroup name="Rewrite.NET"> <section name="Index" type="System.Configuration.NameValueSectionHandler" /> <section name="SimpleSettingsMay1" type="System.Configuration.NameValueSectionHandler" /> <section name="RegExpRule1" type="System.Configuration.NameValueSectionHandler" /> </sectionGroup> <section name="testSection" type="System.Configuration.NameValueSectionHandler" /> </configSections> <testSection> <add key="RegExpRule1" value="RexExpRule.RegExp,RexExpRule" /> <add key="SimpleSettingsMay1" value="RulesEngine.SimpleRule,RulesEngine" /> </testSection> <Rewrite.NET> <Index> <add key="RegExpRule1" value="RexExpRule.RegExp,RexExpRule" /> <add key="SimpleSettingsMay1" value="RulesEngine.SimpleRule,RulesEngine" /> </Index> <!--the actual settings for the rule set for the section--> <SimpleSettingsMay1> <add key="/rewrite.net/webform1.aspx?c=f" value="/rewrite.net/finalpage.aspx" /> </SimpleSettingsMay1> <RegExpRule1> <add key="^/rewrite.net/corp/(.*)" value="/rewrite.net/about/$1" /> </RegExpRule1> </Rewrite.NET> 。。。 </configuration>
3.在代码中读取
NameValueCollection SectionIndex = (NameValueCollection)ConfigurationManager.GetSection("Rewrite.NET/Index");
NameValueCollection SectionIndex = (NameValueCollection)ConfigurationManager.GetSection("testSection");
自定义配置节的处理程序也可以自定义
1.创建自定义类
public class MyHandler : ConfigurationSection { [ConfigurationProperty("myAttrib1", DefaultValue = "Clowns", IsRequired = true)] [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;‘\"|\\", MinLength = 1, MaxLength = 60)] public String MyAttrib1 { get { return (String)this["myAttrib1"]; } set { this["myAttrib1"] = value; } } }
2.在<configSections>中注册对应关系
<configSections> <section name="myCustomSection" type="MyConfigSectionHandler.MyHandler, MyCustomConfigurationHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere" /> <!-- Other <section> and <sectionGroup> elements. --> </configSections>
<myCustomSection myAttrib1="Clowns">
3.读取
MyConfigSectionHandler.MyHandler config = (MyConfigSectionHandler.MyHandler)System.Configuration.ConfigurationManager.GetSection( "myCustomSection");
msdn https://msdn.microsoft.com/zh-cn/library/2tw134k3(v=vs.80).aspx
标签:
原文地址:http://www.cnblogs.com/yy2056/p/4414706.html