示例

 
  1. 下面的代码示例演示如何在创建自定义节时使用 ConfigurationProperty。

 
using System;
using System.Configuration;
using System.Collections;
using System.ComponentModel; namespace ConfigurationPropertyExample
{
// Define a custom section.
// Shows how to use the ConfigurationProperty
// class when defining a custom section.
public sealed class CustomSection : ConfigurationSection
{
// The collection (property bag) that contains
// the section properties.
private static ConfigurationPropertyCollection _Properties; // The FileName property.
private static ConfigurationProperty _FileName; // The Alias property.
private static ConfigurationProperty _Alias; // The MaxUsers property.
private static ConfigurationProperty _MaxUsers; // The MaxIdleTime property.
private static ConfigurationProperty _MaxIdleTime; // CustomSection constructor.
static CustomSection()
{
// Initialize the _FileName property
_FileName =
new ConfigurationProperty("fileName",
typeof(string), "default.txt"); // Initialize the _MaxUsers property
_MaxUsers =
new ConfigurationProperty("maxUsers",
typeof(long), (long)1000,
ConfigurationPropertyOptions.None); // Initialize the _MaxIdleTime property
TimeSpan minTime = TimeSpan.FromSeconds(30);
TimeSpan maxTime = TimeSpan.FromMinutes(5); ConfigurationValidatorBase _TimeSpanValidator =
new TimeSpanValidator(minTime, maxTime, false); _MaxIdleTime =
new ConfigurationProperty("maxIdleTime",
typeof(TimeSpan), TimeSpan.FromMinutes(5),
TypeDescriptor.GetConverter(typeof(TimeSpan)),
_TimeSpanValidator,
ConfigurationPropertyOptions.IsRequired,
"[Description:This is the max idle time.]"); // Initialize the _Alias property
_Alias =
new ConfigurationProperty("alias",
typeof(string), "alias.txt"); // Initialize the Property collection.
_Properties = new ConfigurationPropertyCollection();
_Properties.Add(_FileName);
_Properties.Add(_Alias);
_Properties.Add(_MaxUsers);
_Properties.Add(_MaxIdleTime);
} // Return the initialized property bag
// for the configuration element.
protected override ConfigurationPropertyCollection Properties
{
get
{
return _Properties;
}
} // Clear the property.
public void ClearCollection()
{
Properties.Clear();
} // Remove an element from the property collection.
public void RemoveCollectionElement(string elName)
{
Properties.Remove(elName);
} // Get the property collection enumerator.
public IEnumerator GetCollectionEnumerator()
{
return (Properties.GetEnumerator());
} [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\",
MinLength = 1, MaxLength = 60)]
public string FileName
{
get
{
return (string)this["fileName"];
}
set
{
this["fileName"] = value;
}
} [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\",
MinLength = 1, MaxLength = 60)]
public string Alias
{
get
{
return (string)this["alias"];
}
set
{
this["alias"] = value;
}
} [LongValidator(MinValue = 1, MaxValue = 1000000,
ExcludeRange = false)]
public long MaxUsers
{
get
{
return (long)this["maxUsers"];
}
set
{
this["maxUsers"] = value;
}
} public TimeSpan MaxIdleTime
{
get
{
return (TimeSpan)this["maxIdleTime"];
}
set
{
this["maxIdleTime"] = value;
}
}
}
}

下面的示例摘自上一示例中的代码所用的配置文件。

 
<configuration>
<configSections>
<section name="CustomSection" type="ConfigurationPropertyExample.CustomSection, ConfigurationPropertyExample"
allowDefinition="Everywhere" allowExeDefinition="MachineToApplication"
restartOnExternalChanges="true" />
</configSections>
<CustomSection fileName="override.txt" alias="alias.txt"
maxUsers="1000" maxIdleTime="00:05:00" />
</configuration>

下面的示例演示如何在代码中创建以上部分。

 
// Define a custom section programmatically.
static void CreateSection()
{
try
{
CustomSection customSection; // Get the current configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None); // Create the section entry
// in the <configSections> and the
// related target section in <configuration>.
// Call it "CustomSection2" since the file in this
// example already has "CustomSection".
if (config.Sections["CustomSection"] == null)
{
customSection = new CustomSection();
config.Sections.Add("CustomSection2", customSection);
customSection.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
}
}
catch (ConfigurationErrorsException err)
{
Console.WriteLine(err.ToString());
}
}

自定义的Config节点及使用的更多相关文章

  1. C# 如何获取自定义的config中节点的值,并修改节点的值

    现定义一个方法 DIYConfigHelper.cs using System; using System.Xml; using System.Configuration; using System. ...

  2. 在.net中读写config文件的各种方法(自定义config节点)

    http://www.cnblogs.com/fish-li/archive/2011/12/18/2292037.html 阅读目录 开始 config文件 - 自定义配置节点 config文件 - ...

  3. Spirng boot 启动的时候进行监控检查不通过停止服务与自定义健康监控节点

    基于 spring-boot-starter-actuator   • 前提条件: <dependency>   <groupId>org.springframework.bo ...

  4. 获取或设置config节点值

    ExeConfigurationFileMap 这个类提供了修改.获取指定 config 的功能:新建一个 ExeConfigurationFileMap 的实例 ecf :并设置 ExeConfig ...

  5. C#读取自定义的config

    今天说下C#读写自定义config文件的各种方法.由于这类文章已经很多,但是大多数人举例子都是默认的在app.confg或者web.config进行读写,而不是一般的XML文件,我主要写的是一般的Xm ...

  6. [UE4]CustomAnimationBlueprintNode 自定义动画蓝图节点

    目的:在AnimationBlueprint中使用自定义动画控制节点. 主要过程: 1.      引用相关模块.在Client.Build.cs文件中,PublicDependencyModuleN ...

  7. jquery Ztree v3.5 实例2 自定义显示在节点前的图片

    显示效果如下: 代码如下: <html> <head><title></title></head> <script type=&quo ...

  8. 对 web.config 节点信息进行加密

    记录一下,免得以后再网上找 项目中,数据库访问链接字符串配置在web.config中,明文的,应客户需求需改成密文,so,需要加密. 一开始想的是需要重写configuration什么什么的,最后发现 ...

  9. cdnbest自定义错误显示节点名教程

    在自定义错误里选择js选项,输入: document.write("error!" + hostname); 这是最简单的写法,只显示节点名,如果要显示其他效果,可自已修改js

随机推荐

  1. AVL树之 Java的实现

    AVL树的介绍 AVL树是高度平衡的而二叉树.它的特点是:AVL树中任何节点的两个子树的高度最大差别为1. 上面的两张图片,左边的是AVL树,它的任何节点的两个子树的高度差别都<=1:而右边的不 ...

  2. sublime使用技巧之集成VI

    熟悉开发工具,减少多余的操作流程有助于提高开发效率,而Sublime Text 2是sublime产品的经典版本,因此本文基于Sublime Text 2讲解sublime的使用技巧. VI的主要作用 ...

  3. Java实现发送邮件(可配置)忘记密码,发送邮件

    学过Java基础的应该知道Java里有邮件这一块,不熟悉的话可以简单复习一下 本文章把发送邮件做为可配置可配置文件,这样方便以后维护 一.Maven依赖包 (发送邮件所依赖的jar包) <!-- ...

  4. Python--简单接口测试实例(一)

    适用人员:初学python的测试人员,若对抓包不太清楚的可先学习抓包的知识 接口测试流程:发送请求-->返回响应-->结果判定-->生成报告 案例:下面以[今目标]新建客户为例来进行 ...

  5. 微信公众号网页授权登录--JAVA

    网上搜资料时,网友都说官方文档太垃圾了不易看懂,如何如何的.现在个人整理了一个通俗易懂易上手的,希望可以帮助到刚接触微信接口的你. 请看流程图!看懂图,就懂了一半了: 其实整体流程大体只需三步:用户点 ...

  6. java 回调函数解读

    模块间调用 在一个应用系统中,无论使用何种语言开发,必然存在模块之间的调用,调用的方式分为几种: (1)同步调用 同步调用是最基本并且最简单的一种调用方式,类A的方法a()调用类B的方法b(),一直等 ...

  7. PAT1134:Vertex Cover

    1134. Vertex Cover (25) 时间限制 600 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A vertex ...

  8. Ajax 与服务器通信 验证编号重复

    在最近的一个Web项目中,需要实现一个功能,就是用户在前端输入一个编号,后台需要验证这个编号是否在数据库中已经存在,如果存在就提示用户. 主要用到两个模块.第一:在jsp中添加一个脚本,利用ajax向 ...

  9. redis缓存本地安装教程

    http://www.runoob.com/redis/redis-install.html

  10. js中函数的写法

    js提供了灵活的函数写法,我们常见的函数写法和调用可能是: function ask(){ console.log(1); } ask(); 这样就完成了函数的定义和调用,司空见惯. 还有js里面的匿 ...