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#自定义配置文件节的实现的更多相关文章

  1. C#创建自定义配置节

    在.Net应用程序中,我们经常看到VS为我们生成的项目工程中都会含有nfig或者nfig这样的文件.这个文件就是我们所说的应用程序配置文件.在这个文件里面记述着一些与我们的应用程序相关的信息,如:数据 ...

  2. C#如何使用和开发自定义配置节

    在日常的程序设计中,如何灵活和巧妙地运用配置信息是一个成功的设计师的首要选择.这不仅是为了程序设计得更灵活性和可扩展性,也是为了让你的代码给人以清新的感觉.程序中的配置信息一般放在应用程序的app.c ...

  3. C# 自定义配置文件

    配置文件有两种设置方式,第一种是直接在网站根目录下的web.config中设置:第二种方式是自定义配置文件,在web.config中指定其他配置文件的路径. 第一种:除了在常用的appSettings ...

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

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

  5. 自定义配置文件读取产生的“无法添加已属于该配置的 ConfigurationSection”异常解决办法

    最近在编写一个读写自定义配置文件的功能时遇到一个问题,在初始化的时候读入配置显示出来,修改后把配置回存到配置文件,在回存的时候,先移除配置节,再添加,在添加的时候遇到如下的异常: {"无法添 ...

  6. C#自定义配置文件(一)

    C#自定义配置文件 .NET程序中,经常使用Config文件来配置应用程序中经常使用的值,比如数据库连接字符串.最近项目遇到一个需要配置好多节点在配置文件中的需求.为了使配置节点整洁易维护,在代码调用 ...

  7. thinkphp3.2自定义配置文件

    扩展配置可以支持自动加载额外的自定义配置文件,并且配置格式和项目配置一样. 设置扩展配置的方式如下(多个文件用逗号分隔): // 加载扩展配置文件 'LOAD_EXT_CONFIG' => 'u ...

  8. beego里面自定义配置文件

    beego编译好的exe通过全路径调用会crash,看了半天,发现是解析不到配置文件,研究了下 发现beego自定义配置文件以后,需要手工parse,我表示,以为是自动化的,没想到是半自动化的…… 追 ...

  9. Springboot读取配置文件及自定义配置文件

    1.创建maven工程,在pom文件中添加依赖 <parent> <groupId>org.springframework.boot</groupId> <a ...

随机推荐

  1. elasticsearch index之Translog

    跟大多数分布式系统一样,es也通过临时写入写操作来保证数据安全.因为lucene索引过程中,数据会首先据缓存在内存中直到达到一个量(文档数或是占用空间大小)才会写入到磁盘.这就会带来一个风险,如果在写 ...

  2. 你必须要知道的几个CSS技巧

    有些经典的CSS技巧,我们还是需要记住的,这样可以节省我们大量的时间,下面零度就为大家推荐几个比较好的CSS技巧: 1.在不同页面上使用同样的导航代码 许多网页上都有导航菜单,当进入某页时,菜单上相应 ...

  3. 37.cgi网页交互

    1.Apache的安装 地址:链接:https://pan.baidu.com/s/1kWdSWwZ 密码:nuqo 2.在相应路径下写html如图所示 new.html代码: <html> ...

  4. DbSet<>.Find()

    第一篇为大家带来新的API,DbSet<>.Find(). 过去我们常常用Where或First(FirstOrDefault)方法来查找对应的实体,比如: var people = fr ...

  5. Kinect 开发 —— 姿势识别

    姿势和手势通常会混淆,但是他们是两个不同的概念.当一个人摆一个姿势时,他会保持身体的位置和样子一段时间.但是手势包含有动作,例如用户通过手势在触摸屏上,放大图片等操作. 通常,游戏者很容易模仿指定姿势 ...

  6. spark network-common

    概述 Spark底层使用netty作为节点间通信的桥梁.其实现在common/network-common包中.common/network-common包主要是对netty进行了一层封装,主要是定义 ...

  7. ble_app_hrs心率程序 nrf51822

    所用程序为: H:\keil\ARM\Device\Nordic\nrf51822\Board\pca10001\s110\ble_app_hrs 上面的路径是安装sdk之后生成在keil软件所在目录 ...

  8. PatentTips - Enhancing the usability of virtual machines

    BACKGROUND Virtualization technology enables a single host computer running a virtual machine monito ...

  9. 购买VPS时机房的选择

    购买VPS时机房的选择 对于配置差不多的VPS,距离越近的一般网速越快,所以有必要使用谷歌地图,查看城市的所在位置 比如ChicagoVPS,不时有邮件销售它的VPS,虽然说不上怎么好,但是一般的使用 ...

  10. Codeforces 718C. Sasha and Array(线段树)

    传送门 解题思路: 这道题给了我们一个崭新的角度来看线段树. 我们常常使用的线段树是维护区间的函数的. 这里呢,提示我们线段树其实还可以维护递推. 美好的矩阵递推性质支持了这一功能. 或者说,对于递推 ...