https://msdn.microsoft.com/en-us/library/system.configuration.configurationsection(v=vs.110).aspx

Remarks

You use the ConfigurationSection class to implement a custom section type.

Extend the ConfigurationSection class to provide custom handling and programmatic access to custom configuration sections.

For information about how use custom configuration sections, see How to: Create Custom Configuration Sections Using ConfigurationSection.

A section registers its handling type with an entry in the configSections element.

For an example, see the configuration file excerpt摘录 shown in the Example section.

Note:

In previous versions of the .NET Framework, configuration section handlers were used to make changes to configuration settings programmatically.

Now, all the default configuration sections are represented by classes that extend the ConfigurationSection class.

Notes to Implementers:

You can use a programmatic or a declarative (attributed) coding model to create custom configuration sections:

  • Programmatic model. This model requires that for each section attribute you create a property to get or set its value and add it to the internal property bag of the underlying ConfigurationElement base class.

  • Declarative model. This simpler model, also called the attributed model, allows you to define a section attribute by using a property and decorating it with attributes. These attributes instruct the ASP.NET configuration system about the property types and their default values. With this information, obtained through reflection, the ASP.NET configuration system creates the section property objects and performs the required initialization.

The Configuration class allows programmatic access for editing configuration files.

You can access these files for reading or writing as follows:

Reading. You use GetSection or GetSectionGroup to read configuration information. Note that the user or process that reads must have the following permissions:

  • Read permission on the configuration file at the current configuration hierarchy level.

  • Read permissions on all the parent configuration files.

If your application needs read-only access to its own configuration, it is recommended you use the GetSection overloaded methods in the case of Web applications, or the ConfigurationManager.GetSection method in the case of client applications.

These methods provide access to the cached configuration values for the current application, which has better performance than the Configuration class.

Note:

If you use a static GetSection method that takes a path parameter, the path parameter must refer to the application in which the code is running;

otherwise, the parameter is ignored and configuration information for the currently-running application is returned.

Writing. You use one of the Save methods to write configuration information. Note that the user or process that writes must have the following permissions:

  • Write permission on the configuration file and directory at the current configuration hierarchy level.

  • Read permissions on all the configuration files.

The following example shows how to implement a custom section programmatically.

For a complete example that shows how to implement and use a custom section implemented using the attributed model, seeConfigurationElement.

删除section

如上图所示,需要删除名为fiftyOne的sectiongroup。并且添加一个section为ajaxControlToolKit的section。

 [TestFixture]
class KenticoUpgrade7To11Test
{
[Test]
public void Test()
{
var filePath = @"/";
var configuration = WebConfigurationManager.OpenWebConfiguration(filePath,"Test"); var sectionGroups = configuration.SectionGroups;
var sections = configuration.Sections; var sectionGroupToDelete = "fiftyOne";
sectionGroups.Remove(sectionGroupToDelete); var sectionToAdd = "ajaxControlToolkit";
ConfigurationSection section = sections.Get(sectionToAdd);
var ajaxControlToolkitConfigSection = section as AjaxControlToolkitConfigSection;
if (ajaxControlToolkitConfigSection == null)
{
//add
ajaxControlToolkitConfigSection = new AjaxControlToolkitConfigSection();
sections.Add(sectionToAdd, ajaxControlToolkitConfigSection);
}
else
{
//update
}
ajaxControlToolkitConfigSection.SectionInformation.Type = "AjaxControlToolkit.AjaxControlToolkitConfigSection, AjaxControlToolkit";
ajaxControlToolkitConfigSection.SectionInformation.RequirePermission = false; configuration.Save();
}
}

ConfigurationSection的更多相关文章

  1. ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节集合

    核心代码 using System; using System.Data; using System.Configuration; using System.Web; using System.Web ...

  2. ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节

    主要代码,一定要继续System.Configuration.ConfigurationSection,具体的节点名称可以自行修改 using System; using System.Data; u ...

  3. c# ConfigurationSection

    怎么把自己的配置文件配置到app.config中? 方案1:在app.config中添加 <!--应用配置--> <appSettings configSource="Co ...

  4. 自定义ConfigurationSection,创建多个嵌套的ConfigurationElementCollection节点

    由于接口地址都是固定的,所以想到使用自定义节点,来将接口都配置到web.config中. 很快,v1.0版本出炉: public class RequestConfigSection : Config ...

  5. .Net 配置文件--继承ConfigurationSection实现自定义处理类处理自定义配置节点

    除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...

  6. .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点

    除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果. 首先说下.Net配置文件中一个 ...

  7. [转]通过继承ConfigurationSection,在web.config中增加自定义配置

    本文转自:http://www.blue1000.com/bkhtml/2008-02/55810.htm 前几天写了一篇使用IConfigurationSectionHandler在web.conf ...

  8. .Net 配置文件——继承ConfigurationSection实现自己定义处理类处理自己定义配置节点

    除了使用继承IConfigurationSectionHandler的方法定义处理自己定义节点的类.还能够通过继承ConfigurationSection类实现相同效果. 首先说下.Net配置文件里一 ...

  9. [2014-02-19]ConfigurationSection:让web.config配置更有条理

    本文针对新手 使用Web.config的配置信息,一般都习惯于使用 ConfigurationManager.AppSettings["ConfigKey"] 当程序不断迭代,开发 ...

  10. 使用 ConfigurationSection 创建自定义配置节

    我们可以通过用自己的 XML 配置元素来扩展标准的 ASP.NET 配置设置集,要完成这一功能,我们必须实现继承System.Configuration.ConfigurationSection 类来 ...

随机推荐

  1. (转)彻底学会使用epoll(一)——ET模式实现分析

    注:之前写过两篇关于epoll实现的文章,但是感觉懂得了实现原理并不一定会使用,所以又决定写这一系列文章,希望能够对epoll有比较清楚的认识.是请大家转载务必注明出处,算是对我劳动成果的一点点尊重吧 ...

  2. FreeRTOS系列第13篇---FreeRTOS内核控制

    内核控制的一些功能须要移植层提供,为了方便移植.这些API函数用宏来实现,比方上下文切换.进入和退出临界区.禁止和使能可屏蔽中断.内核控制函数还包含启动和停止调度器.挂起和恢复调度器以及用于低功耗模式 ...

  3. 软工视频总结Part Three

    软件需求分析 任务 解决目标系统"做什么"问题 深入描写叙述软件的功能和性能 确定软件涉及到的约束和软件接口 定义软件的其它有效需求 特点 一致性.完整性.限时性.有效性.可验证性 ...

  4. 网线直连Window和Ubuntu

      找根网线直接连接两台电脑. 然后设置对应的网络到相同的网关和Mask下面.  检测能否ping通就可以直连了.   Ubuntu设置网络后可在IP设置界面里设置Route, 选择直连网线仅用于连接 ...

  5. BZOJ 2190 欧拉函数

    思路: 递推出来欧拉函数 搞个前缀和 sum[n-1]*2+3就是答案 假设仪仗队是从零开始的 视线能看见的地方就是gcd(x,y)=1的地方 倒过来一样 刨掉(1,1) 就是ans*2+1 再加一下 ...

  6. Windows窗体应用布局详解

    上回我们已经会用基本的控件创建Windows窗体应用,这才我们再来认识一些高级控件并使用ADO.NET技术连接数据库来创建功能更坚强大的窗体应用! 菜单栏控件MenuStrip .NET中提供了一个M ...

  7. Swift学习笔记(10):类和结构体

    目录: 基本 属性 方法 下标 继承 基本 使用class和struct关键字定义类和结构体. ・类是引用类型,结构体和枚举是值类型 ・值类型被赋予给一个变量.常量或被传递给一个函数时,已值拷贝方式传 ...

  8. handsontable在线编辑excel扩展功能-踩坑篇

    简述 先说一下背景,之所以封装handsontable插件,是因为公司要实现在线编辑导入excel文件的功能,然后我就找到了这个功能强大的插件handsontable. 具体功能 除了handsont ...

  9. 第三方库requests

    requests库 # 1.记得安装 第三方 模块 requests # pip install requests import requests url = 'http://www.baidu.co ...

  10. tigergao

    互联网从业 6 年.前码农&DBA,现运维&电商创业者,也在做自媒体.终生学习者. 运营微信公众号:高哥咋么看 感兴趣的朋友们可以订阅.