自定义的Config节点及使用
下面的代码示例演示如何在创建自定义节时使用 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节点及使用的更多相关文章
- C# 如何获取自定义的config中节点的值,并修改节点的值
现定义一个方法 DIYConfigHelper.cs using System; using System.Xml; using System.Configuration; using System. ...
- 在.net中读写config文件的各种方法(自定义config节点)
http://www.cnblogs.com/fish-li/archive/2011/12/18/2292037.html 阅读目录 开始 config文件 - 自定义配置节点 config文件 - ...
- Spirng boot 启动的时候进行监控检查不通过停止服务与自定义健康监控节点
基于 spring-boot-starter-actuator • 前提条件: <dependency> <groupId>org.springframework.bo ...
- 获取或设置config节点值
ExeConfigurationFileMap 这个类提供了修改.获取指定 config 的功能:新建一个 ExeConfigurationFileMap 的实例 ecf :并设置 ExeConfig ...
- C#读取自定义的config
今天说下C#读写自定义config文件的各种方法.由于这类文章已经很多,但是大多数人举例子都是默认的在app.confg或者web.config进行读写,而不是一般的XML文件,我主要写的是一般的Xm ...
- [UE4]CustomAnimationBlueprintNode 自定义动画蓝图节点
目的:在AnimationBlueprint中使用自定义动画控制节点. 主要过程: 1. 引用相关模块.在Client.Build.cs文件中,PublicDependencyModuleN ...
- jquery Ztree v3.5 实例2 自定义显示在节点前的图片
显示效果如下: 代码如下: <html> <head><title></title></head> <script type=&quo ...
- 对 web.config 节点信息进行加密
记录一下,免得以后再网上找 项目中,数据库访问链接字符串配置在web.config中,明文的,应客户需求需改成密文,so,需要加密. 一开始想的是需要重写configuration什么什么的,最后发现 ...
- cdnbest自定义错误显示节点名教程
在自定义错误里选择js选项,输入: document.write("error!" + hostname); 这是最简单的写法,只显示节点名,如果要显示其他效果,可自已修改js
随机推荐
- 排序算法入门之选择排序-Java实现
本文参考http://blog.csdn.net/m0_37568091/article/details/78023705 选择排序是先从对象数组中选出最小的放在第一个位置,再从剩下的元素中选择次小的 ...
- 推荐eclipse插件Properties Editor(转)
Properties Editor 是一款properties文件编辑器. 需求:一般我们在做“国际化”功能时,我们需要properties中文表示方式用unicode表示.eclipse默认prop ...
- Js 实现自定义事件
var Event = { on: function (eventName, callback) { if (!this[eventName]) { this[eventName] = []; } t ...
- 【.NET Core】ASP.NET Core之IdentityServer4(1):快速入门
[.NET Core]ASP.NET Core之IdentityServer4 本文中的IdentityServer4基于上节的jenkins 进行docker自动化部署. 使用了MariaDB,EF ...
- Redis实现简单消息队列
http://www.jianshu.com/p/9c04890615ba 任务异步化 打开浏览器,输入地址,按下回车,打开了页面.于是一个HTTP请求(request)就由客户端发送到服务器,服务器 ...
- Android 众多的布局属性详解
http://www.open-open.com/lib/view/open1328686184311.html Android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面这篇文章结合了 ...
- Hbase出现ERROR: Can't get master address from ZooKeeper; znode data == null解决办法
问题描述如下: hbase(main)::> list TABLE ERROR: Can't get master address from ZooKeeper; znode data == n ...
- Java虚拟机-垃圾收集器
垃圾收集器(Garbage Collection, GC)的诞生引导出了三个问题: 哪些内存需要回收? 什么时候回收? 如何回收? 对于线程独占的三个区域(程序计数器.虚拟机栈.本地方法栈)不用过多的 ...
- windows和centos下安装ActiveMQ
版本:apache-activemq-5.10.2-bin.zip (版本5.11+需要jdk7+) 官网: http://activemq.apache.org/download.h ...
- NGINX按天生成日志文件的简易配置
NGINX按天生成日志文件的简易配置 0x01 最近后端童鞋遇到一个小需求,拆分nginx生成的log文件,最好是按天生成,看着她还有很多bug待改的状态,我说这个简单啊,我来吧.曾经搞node后端的 ...