C#自定义配置文件节的实现
1.配置文件:(注意configSections必须放在最上面否则会报错)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="UserDefinedConfiguration" type="ConsoleApplication9.UserDefinedConfiguration,ConsoleApplication9, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
allowLocation="true"
allowDefinition="Everywhere"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<UserDefinedConfiguration id="12" name="hecong" firstProperty="property2">
<myChildSection url="www.163.com" addr="不知道"></myChildSection><!--子节点-->
</UserDefinedConfiguration>
</configuration>
2.配置文件的主类型也就是配置文件section中type所标记的类(注意应用configuration,dll):
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication9
{
/// <summary>
/// 自定义配置文件节点类型
/// </summary>
public class UserDefinedConfiguration: ConfigurationSection
{
private static UserDefinedConfiguration setting;
public static UserDefinedConfiguration Setting
{
get
{
if (setting == null)
setting = (UserDefinedConfiguration)ConfigurationManager.GetSection("UserDefinedConfiguration");
return setting;
}
} [ConfigurationProperty("id", DefaultValue = "", IsRequired = true)]
[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = , MaxLength = )]//这个属性只是不出现引号中的字符
public long Id
{
get { return (long)this["id"]; }
set { this["id"] = value; }
} [ConfigurationProperty("name", DefaultValue = "Lily", IsRequired = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
} [ConfigurationProperty("firstProperty", DefaultValue = "Property1", IsRequired = true)]
public string FirstProperty
{
get { return (string)this["firstProperty"]; }
set { this["firstProperty"] = value; }
} [ConfigurationProperty("myChildSection")]
public UrlConfigurationElement MyChildSection//子节点
{
get
{ return (UrlConfigurationElement)this["myChildSection"]; }
set
{ this["myChildSection"] = value; }
}
}
}
3.子节点类型:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication9
{
public class UrlConfigurationElement: ConfigurationElement
{
[ConfigurationProperty("url", DefaultValue = "www.baidu.com", IsRequired = true)]
public string Url
{
get { return (string)this["url"]; }
set { this["url"] = value; }
} [ConfigurationProperty("addr", DefaultValue = "Lily", IsRequired = true)]
public string Addr
{
get { return (string)this["addr"]; }
set { this["addr"] = value; }
}
}
}
4.后续添加key,value 和多个add节点
C#自定义配置文件节的实现的更多相关文章
- C#创建自定义配置节
在.Net应用程序中,我们经常看到VS为我们生成的项目工程中都会含有nfig或者nfig这样的文件.这个文件就是我们所说的应用程序配置文件.在这个文件里面记述着一些与我们的应用程序相关的信息,如:数据 ...
- C#如何使用和开发自定义配置节
在日常的程序设计中,如何灵活和巧妙地运用配置信息是一个成功的设计师的首要选择.这不仅是为了程序设计得更灵活性和可扩展性,也是为了让你的代码给人以清新的感觉.程序中的配置信息一般放在应用程序的app.c ...
- C# 自定义配置文件
配置文件有两种设置方式,第一种是直接在网站根目录下的web.config中设置:第二种方式是自定义配置文件,在web.config中指定其他配置文件的路径. 第一种:除了在常用的appSettings ...
- 使用 ConfigurationSection 创建自定义配置节
我们可以通过用自己的 XML 配置元素来扩展标准的 ASP.NET 配置设置集,要完成这一功能,我们必须实现继承System.Configuration.ConfigurationSection 类来 ...
- 自定义配置文件读取产生的“无法添加已属于该配置的 ConfigurationSection”异常解决办法
最近在编写一个读写自定义配置文件的功能时遇到一个问题,在初始化的时候读入配置显示出来,修改后把配置回存到配置文件,在回存的时候,先移除配置节,再添加,在添加的时候遇到如下的异常: {"无法添 ...
- C#自定义配置文件(一)
C#自定义配置文件 .NET程序中,经常使用Config文件来配置应用程序中经常使用的值,比如数据库连接字符串.最近项目遇到一个需要配置好多节点在配置文件中的需求.为了使配置节点整洁易维护,在代码调用 ...
- thinkphp3.2自定义配置文件
扩展配置可以支持自动加载额外的自定义配置文件,并且配置格式和项目配置一样. 设置扩展配置的方式如下(多个文件用逗号分隔): // 加载扩展配置文件 'LOAD_EXT_CONFIG' => 'u ...
- beego里面自定义配置文件
beego编译好的exe通过全路径调用会crash,看了半天,发现是解析不到配置文件,研究了下 发现beego自定义配置文件以后,需要手工parse,我表示,以为是自动化的,没想到是半自动化的…… 追 ...
- Springboot读取配置文件及自定义配置文件
1.创建maven工程,在pom文件中添加依赖 <parent> <groupId>org.springframework.boot</groupId> <a ...
随机推荐
- vim 插件之commentary
下载地址 http://www.vim.org/scripts/script.php?script_id=3695 如果是使用vundle来管理这个插件的话,你可以添加如下配置 tpope/vim-c ...
- 解决create-react-app 后 npm start 中出现 的webpack版本问题和webpack-dev-server的版本问题
利用VSCode搭建react的脚手架运行环境的时候.create-react-app之后npm start出现如下图的问题: There might be a problem with the pr ...
- 洛谷 P1957 口算练习题
洛谷 P1957 口算练习题 题目描述 王老师正在教简单算术运算.细心的王老师收集了i道学生经常做错的口算题,并且想整理编写成一份练习. 编排这些题目是一件繁琐的事情,为此他想用计算机程序来提高工作效 ...
- Install Docker Mac OS X
检查 Mac OS version 要求必须是 OS X 10.6 Snow Leopard or newer to run Boot2Docker 安装 Boot2Docker 列表内容 下载地址: ...
- JavaScript作用域闭包(你不知道的JavaScript)
JavaScript闭包.是JS开发project师必须深入了解的知识. 3月份自己曾撰写博客<JavaScript闭包>.博客中仅仅是简单阐述了闭包的工作过程和列举了几个演示样例,并没有 ...
- Windows 98 二十岁了,这些功能都是从它开始的(虽然 Windows 98 不如 Windows 95 那样具有革命性,但完成度更高,更加成熟。到最后还是:相见不如怀念。)
1998 年 6 月 25 日午夜,美国著名连锁零售店 CompUSA 门外挤满了狂热的消费者和媒体,他们在等待一款软件发售:Windows 98,即使明知它要到当天早上才正式上市. ▲ 在 Comp ...
- 将 php 转换/编译为 EXE
将 php 转换/编译为 EXE 本文仅仅是将原文用谷歌作了翻译,原文来源于 http://stackoverflow.com 资料来源 http://stackoverflow.com/quest ...
- Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
if (isset($array[2])){ 抛出错误 Cannot use isset() on the result of an expression (you can use "nu ...
- [ Eclipse ] [ Problem ] Eclipse 無法開啟問題
因為 Eclipse 在設定環境的過程掛掉太多次,擷取一些網路上優秀的文章當作備份 http://www.ewdna.com/2013/12/Eclipse-Loading-Workbench.htm ...
- 一文了解sun.misc.Unsafe
Java语言和JVM平台已经度过了20岁的生日.它最初起源于机顶盒.移动设备和Java-Card,同时也应用在了各种服务器系统中,Java已成为物联网(Internet of Things)的通用语言 ...