config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="FileDepend" type="TestConsole.FileDepend,TestConsole"/>
  </configSections>
  <FileDepend>
    <RootDir path="c:\"></RootDir>
    <Public>
      <element file="/1.txt"></element>
      <element file="/2.txt"></element>
    </Public>
    <Modules>
      <module name="legend">
        <element file="/3.txt"></element>
        <element file="/4.txt"></element>
      </module>
      <module name="bookmark">
        <element file="/5.txt"></element>
        <element file="/6.txt"></element>
      </module>
    </Modules>
  </FileDepend>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
  </startup>
</configuration>

FileDepend.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace TestConsole
{
    public class FileDepend : ConfigurationSection
    {
        [ConfigurationProperty("RootDir")]
        private RootDirElement _RootDir => (RootDirElement)base["RootDir"];

        [ConfigurationProperty("Public")]
        private FilesCollection PublicFilesCollection => ((FilesCollection)(base["Public"]));

        public string RootDir => _RootDir.Name;

        [ConfigurationProperty("Modules")]
        public ModulesCollection ModulesCollection => ((ModulesCollection)(base["Modules"]));

        public IEnumerable<string> PublicFiles => from FileElement v in PublicFilesCollection select v.Name;
    }

    public class RootDirElement : ConfigurationElement
    {
        [ConfigurationProperty("path", DefaultValue = "", IsKey = true, IsRequired = true)]
        public string Name => (string)base["path"];
    }

    public class FileElement : ConfigurationElement
    {
        [ConfigurationProperty("file", DefaultValue = "", IsKey = true, IsRequired = true)]
        public string Name => (string)base["file"];
    }
    public class ModuleElement : ConfigurationElement
    {
        [ConfigurationProperty("name", DefaultValue = "", IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return (string)base["name"]; }
            set { base["name"] = value; }
        }
        [ConfigurationProperty("", IsDefaultCollection = true)]
        private FilesCollection Element => (FilesCollection)base[""];

        public IEnumerable<string> Files => from FileElement file in Element select file.Name;
    }
    [ConfigurationCollection(typeof(ModuleElement))]
    public class FilesCollection : ConfigurationElementCollection
    {
        internal const string PropertyName = "element";

        public override ConfigurationElementCollectionType CollectionType => ConfigurationElementCollectionType.BasicMapAlternate;

        protected override string ElementName => PropertyName;

        protected override bool IsElementName(string elementName)
        {
            return elementName.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase);
        }

        public override bool IsReadOnly()
        {
            return false;
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new FileElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((FileElement)(element)).Name;
        }

        public FileElement this[int idx] => (FileElement)BaseGet(idx);
        public new FileElement this[string idx] => (FileElement)BaseGet(idx);
    }
    [ConfigurationCollection(typeof(ModuleElement))]
    public class ModulesCollection : ConfigurationElementCollection
    {
        internal const string PropertyName = "module";

        public override ConfigurationElementCollectionType CollectionType => ConfigurationElementCollectionType.BasicMapAlternate;
        protected override string ElementName => PropertyName;

        protected override bool IsElementName(string elementName)
        {
            return elementName.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase);
        }

        public override bool IsReadOnly()
        {
            return false;
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ModuleElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ModuleElement)(element)).Name;
        }

        public ModuleElement this[int idx] => (ModuleElement)BaseGet(idx);
        public new ModuleElement this[string idx] => (ModuleElement)BaseGet(idx);
    }
}

run:

static void Main(string[] args)
        {
            var v = ConfigurationManager.GetSection("FileDepend") as FileDepend;
            var rootDir = v.RootDir;
            var publicFiles = v.PublicFiles;
            var legendFiles = v.ModulesCollection["legend"].Files;
            Console.WriteLine(rootDir);
            publicFiles.ToList().ForEach(Console.WriteLine);
            legendFiles.ToList().ForEach(Console.WriteLine);
            Console.ReadLine();
        }

Create a custom configSection in web.config or app.config file的更多相关文章

  1. 说说Web.Config与App.Config

    说到web.config和app.config大家都很熟悉,我们都叫他们配置文件,平时用的多,注意的少.两个有啥区别呢,很简单,一句话:如果是web程序,如webform项目类型和mvc项目类型就是w ...

  2. 在Web.config或App.config中的添加自定义配置

    .Net中的System.Configuration命名空间为我们在web.config或者app.config中自定义配置提供了完美的支持.最近看到一些项目中还在自定义xml文件做程序的配置,所以忍 ...

  3. 修改和获取web.config或app.config文件appSettings配置节中的Add里的value属性 函数

    1: /// <summary> 2: /// 修改web.config或app.config文件appSettings配置节中的Add里的value属性 3: /// </summ ...

  4. .NET下对Web.config与App.Config的增删改操作的代码

    把代码过程常用的内容做个收藏,下边代码段是关于 .NET下对Web.config与App.Config的增删改操作的代码. <?xml version="1.0" encod ...

  5. 在Web.config或App.config中的添加自定义配置 <转>

        .Net中的System.Configuration命名空间为我们在web.config或者app.config中自定义配置提供了完美的支持.最近看到一些项目中还在自定义xml文件做程序的配置 ...

  6. 一个web.Config或app.Config自定义段configSections的示例

    一个web.Config或app.Config自定义段configSections的示例 越来越觉得,直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml ...

  7. .net分布在指定文件夹的web.confgi或者app.config

    .Net里面,ConfigurationManager默认读取的是Web.config或者App.config但是,什么都放在这两个文件里面,感觉太多了,也不好管理配置.于是参考了下别人的资料,自己写 ...

  8. 一个web.Config或app.Config自定义段configSections的示例--转

    直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml配置文件,简洁方便得多.这两个配置文件不仅有常见的connectionStrings和appSetti ...

  9. 配置文件(Machine.config、Web.config、App.config)

    Machine.config1.该文件在Windows目录下\Microsoft.net\framework\[version]\Config\2.为了提高性能,该文件只包含不同于默认值的设置.并且定 ...

随机推荐

  1. lint使用简介

    LINT工具是一种软件质量保证工具,许多国外的大型专业软件公司,如微软公司,都把它作为程序检查工具,在程序合入正试版本或交付测试之前一定要保证通过了LINT检查,他们要求软件工程师在使用LINT时要打 ...

  2. ios jsbrige

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 在Centos 5.x或6.x上安装RHEL EPEL Repo

    本文介绍了如何在CentOS 5.x或者CentOS 6.x的系统上使用Fedora Epel repos一个第三方repo:remi资源库.这些资源包并不是天然地支持CentOS,但是提供了很多流行 ...

  4. [置顶] android利用jni调用第三方库——第二篇——编写库android程序直接调用第三方库libhello.so

    0:前言 1:本文主要作为丙方android公司的身份来写 2:作者有不对的地方,请指出,谢谢 [第一篇:android利用jni调用第三方库——编写库libhello.so] [第二篇:androi ...

  5. 奔五的人,准备学习iOS开发

    这些年一直在java/web/android方面折腾,去年最终换成了apple的设备,本想就開始折腾iOS,却始终没能进入状态. 从今天開始,本人宣布:正式进入iOS/xcode 5的编程学习中,也希 ...

  6. Tomcat使用startup启动,一闪而过,如何查看出错信息

    解决办法:打开startup.bat,通过使用run命令在原有启动窗口显示出错信息. 在startup.bat中找到:call "%EXECUTABLE%" start %CMD_ ...

  7. 【转】CoreData以及MagicalRecord (一)

    先粗略的了解下CoreData中的一些核心概念 1. CoreData 的核心概念 先上两幅关键的概念图 (1)NSManagedObjectModel 托管对象模型(MOM)是描述应用程序的数据模型 ...

  8. 有关std::map和std::vector的使用

    先说map吧. 最需要注意的就是:用下标访问map中的元素时,与使用下标访问vector的行为截然不同! 用下标访问不存在的元素时,将导致在map容器中添加一个新的元素,它的键即为该下标! 然而很多时 ...

  9. MySQL varchar和char类型

    varchar和char是两种最主要的字符串类型.不幸的是,很难精确地解释这些值是怎么储存在磁盘和内存中的,因为这根存储引擎的具体实现有关.下面的描述假设使用的存储引擎是InnoDB或者MyISAM. ...

  10. [置顶] 修改Android开机画面之rle制作

    第一个动画: Bootloader开机使用的图片是linux.bmp,长高分别是120*120,色度是32,所以可以拿相同格式图片替换,进行编译烧写即可. 第二个图片:内核logo 这个修改方式,我在 ...