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. 最小生成树-并查集-Kruskal-zoj-2048-special judge

    Highways description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a ...

  2. 中科燕园GIS外包--移动GIS

    移动GIS恰逢其时 得益于移动智能终端的普及和移动互联网的发展,伴随着GIS技术的发展和应用的深入.越来越多的企业和普通消费者開始体会到移动GIS的巨大潜力和价值. 移动GIS轻便灵活,受众面广.随时 ...

  3. 【Android界面实现】使用GestureOverlayView控件实现手势识别

    在Android开发中,我们不光能够使用已有的实现方式.并且,我们还能够利用Android这个智能手机平台.实现一些比較有特色的功能. 本篇文章介绍使用GestureOverlayView这个控件.实 ...

  4. jquery实现瀑布流效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. centos的dns配置总结

    找的一篇好的存根 DNS正反向解析 DNS(Domain Name Service)域名服务.就是域名解析服务器.所谓名称解析的过程就是某个应用程序基于某个搜索键在指定的数据库中查询.而后查询到某些对 ...

  6. 洛谷——P2695 骑士的工作

    https://www.luogu.org/problem/show?pid=2695 题目背景 你作为一个村的村长,保卫村庄是理所当然的了.今天,村庄里来了一只恶龙,他有n个头,恶龙到处杀人放火.你 ...

  7. Codeforces 441 B. Valera and Fruits

    B. Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. [NOI.AC#40]Erlang

    链接 题解 显然,最多抽2个集合 如果一直抽一个,前提是该集合有重复的,答案是不同元素的个数+1 如果抽两个,那么最坏情况下,在一个集合中抽到某一个数的次数是这个集合不同元素的个数(因为抽不到重复的) ...

  9. 转:Java的一道面试题----静态变量初始化过程

    public class Test{ private static Test tester = new Test(); //step 1 private static int count1; //st ...

  10. Spring中的AOP注解方式和XML方式

    应掌握内容:1. AOP的全名2. AOP的实现原理[静态代理和动态代理]3. 注解方式的配置4. 通知类型     A. 每种通知的特点和使用方式    B. 获取各种数据,方便日后操作5. 执行表 ...