自定义的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并发之可见性与原子性:Syncronized和volatile
转载:http://blog.csdn.net/guyuealian/article/details/52525724 在说明Java多线程内存可见性之前,先来简单了解一下Java内存模型. ...
- valid palindrome(回文)
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- JAVA堆栈的区别
1. 栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆. 2. 栈的优势是,存取速度 ...
- SQL Server 远程更新目标表数据
分享一个远程更新目标库数据的存储过程,适用于更新列名一致,主键为Int类型,可远程链接的数据库. ** 温馨提示:如需转载本文,请注明内容出处.** 本文连接:http://www.cnblogs.c ...
- Django时区设置的郁闷
第一次在windows下看到这个设置的时候,就设置成Ubuntu上时区设置的字符串“Asia/Shanghai”,结果报错通不过,最后记不清楚从哪儿查的,改成GMT+8. 最近把应用放到Linux上做 ...
- springboot中使用分页,文件上传,jquery的具体步骤(持续更新)
分页: pom.xml 加依赖 <dependency> <groupId>com.github.pagehelper</groupId> <arti ...
- mysql 给表添加唯一约束、联合唯一约束,指定唯一约束的名字
表结构 FIELD TYPE COLLATION NULL KEY DEFAULT Extra PRIVILEGES ...
- Unity3d Mecanim动画系统Animator学习笔记
1. unity3d Mecanim学习 Unity3d新版动画系统网上的介绍很多,但多是流水笔记,外人很难看明白,后来我 终于找到介绍的比较明白的两个作者,特别感谢.一个是58开发网的乐天老师,课 ...
- Python_正则表达式样例
''' 正则表达式是字符串处理的有力工具和技术,正则表达式使用预定义的特定模式去匹配一类具有共同特征的字符串, 主要用于字符串处理,可以快速.准确地完成复杂的查找.替换等处理要求. 正则表达式由元字符 ...
- 微信小程序的初窥实践
最近,小程序正式上线,各企业都纷纷开发,本博主看下其中奥秘, 首先得去微信公众平台(https://mp.weixin.qq.com/)注册一个小程序账号(以前注册过公众号的账号不可使用) 备注:注册 ...