Create a custom configSection in web.config or app.config file
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的更多相关文章
- 说说Web.Config与App.Config
说到web.config和app.config大家都很熟悉,我们都叫他们配置文件,平时用的多,注意的少.两个有啥区别呢,很简单,一句话:如果是web程序,如webform项目类型和mvc项目类型就是w ...
- 在Web.config或App.config中的添加自定义配置
.Net中的System.Configuration命名空间为我们在web.config或者app.config中自定义配置提供了完美的支持.最近看到一些项目中还在自定义xml文件做程序的配置,所以忍 ...
- 修改和获取web.config或app.config文件appSettings配置节中的Add里的value属性 函数
1: /// <summary> 2: /// 修改web.config或app.config文件appSettings配置节中的Add里的value属性 3: /// </summ ...
- .NET下对Web.config与App.Config的增删改操作的代码
把代码过程常用的内容做个收藏,下边代码段是关于 .NET下对Web.config与App.Config的增删改操作的代码. <?xml version="1.0" encod ...
- 在Web.config或App.config中的添加自定义配置 <转>
.Net中的System.Configuration命名空间为我们在web.config或者app.config中自定义配置提供了完美的支持.最近看到一些项目中还在自定义xml文件做程序的配置 ...
- 一个web.Config或app.Config自定义段configSections的示例
一个web.Config或app.Config自定义段configSections的示例 越来越觉得,直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml ...
- .net分布在指定文件夹的web.confgi或者app.config
.Net里面,ConfigurationManager默认读取的是Web.config或者App.config但是,什么都放在这两个文件里面,感觉太多了,也不好管理配置.于是参考了下别人的资料,自己写 ...
- 一个web.Config或app.Config自定义段configSections的示例--转
直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml配置文件,简洁方便得多.这两个配置文件不仅有常见的connectionStrings和appSetti ...
- 配置文件(Machine.config、Web.config、App.config)
Machine.config1.该文件在Windows目录下\Microsoft.net\framework\[version]\Config\2.为了提高性能,该文件只包含不同于默认值的设置.并且定 ...
随机推荐
- 转:CSS3 Flexbox 布局介绍
转:CSS3 Flexbox 布局介绍 Flexbox是一个用于页面布局的全新CSS3模块功能.它可以把列表放在同一个方向(从左到右或从上到下排列),并且让这些列表能延伸到占用可用的空间.较为复杂的布 ...
- Adobe Acrobat XI Pro 官方下载及安装破解
Adobe公司推出的PDF 格式是一种全新的电子文档格式.借助 Acrobat ,您几乎可以用便携式文档格式 (Portable Document Format ,简称 PDF) 出版所有的文档. P ...
- ARM Cortex M3(V7-M架构)硬件启动程序 一
Cortex-m3启动代码分析笔记 启动代码文件名是STM32F10X.S,它的作用先总结下,然后再分析. 启动代码作用一般是: 1)堆和栈的初始化: 2)中断向量表定义: 3)地址重映射及中断向量表 ...
- iOS 4.2 SDK安装
iOS 4.2 SDK安装 ◆系统版本:10.6.5 ◆硬件配置: ◆10G空间,3.5G内存,显卡:GMA950 Xcode 3.2.5和iOS 4.2 SDK下载:http://developer ...
- 删除select中所有option选项
这样写 <select id="search"> <option>baidu</option> <option>sogou</ ...
- 如何在Objective-C中实现链式语法?
在接触到开源项目 Masonry 后,里面的布局约束的链式写法让我颇感兴趣,就像下面这样: 1 2 3 4 5 6 7 8 UIEdgeInsets padding = UIEdgeInsetsMak ...
- 总结:C#变量,占位符等相关知识
新年耽误了不少时间,好久没认真的坐下来学习了,新年也快完了,又要开始正式学习了,按着视频教学学习,用了一天的时间,学习了下简单的变量及其相关的输入输出和应用,学了几种最基本的类型: int(整型) c ...
- SQL学习之联结表的使用
1.简介:"联结(join)表"是SQL最强大的功能之一.联结是利用SQL的SELECT能执行的最重要的操作,很好地理解联结及其语法是学习SQL的极为重要的部分! 在能够有效的使用 ...
- .Net平台-MVP模式再探(二)
PS: 本文与 上一遍文章 没有什么必然的联系,可以说是对于MVP的一定的加深,或许在理解上比上一篇多有点难度. 正文 一.简单讲讲MVP是什么玩意儿 如果从层次关系来讲,MVP属于P ...
- BZOJ 3231: [Sdoi2008]递归数列( 矩阵快速幂 )
矩阵乘法裸题..差分一下然后用矩阵乘法+快速幂就可以了. ----------------------------------------------------------------------- ...