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.为了提高性能,该文件只包含不同于默认值的设置.并且定 ...
随机推荐
- 编程珠玑I算法总结
主要是根据编程珠玑后面的Algorithm附录总结了一下这本书里面的经典算法. 1 辗转相减求最大公约数 思想:最大公约数能整除i和j,则其一定也能整除i-j(if i>j) int gcd(i ...
- HDU 5972 Regular Number(ShiftAnd+读入优化)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5972 [题目大意] 给出一个字符串,找出其中所有的符合特定模式的子串位置,符合特定模式是指,该子串 ...
- ADB几种常见的错误及解决方法
下面列举出几种常见的错误及解决方法. Q1:无效的安装包,安装包已损坏[INSTALL_FAILED_INVALID_APK] A1:请检查安装包是否完整.如果是xpk包,可以通过 手动安装xpk来检 ...
- oracle sql命令行中上下左右使用
yum -y install readline,rlwrap
- 代码中添加事务控制 VS(数据库存储过程+事务) 保证数据的完整性与一致性
做人事档案的系统考虑到数据的安全性与一致性,毕竟是要对外上线.真正投入使用的项目,数据库的可靠性与安全性上我们开发人员要考虑的就很多了,记得做机房收费系统时注册新卡是自己为了简单,写成了一个存储过程( ...
- Java实现串口通信的小样例
用Java实现串口通信(windows系统下),须要用到sun提供的串口包 javacomm20-win32.zip.当中要用到三个文件,配置例如以下: 1.comm.jar放置到 JAVA_HOME ...
- 深入理解Linux网络技术内幕——中断与网络驱动程序
接收到帧时通知驱动程序 在网络环境中.设备(网卡)接收到一个数据帧时,须要通知驱动程序进行处理. 有一下几种通知机制: 轮询: 内核不断检查设备是否有话要说.(比較耗资源,但在一些情况 ...
- JavaScript之<script>标签简介
向html页面中插入JavaScrpt的主要方法,就是使用<script>元素,下面是Html 4.01为<script>定义的6个属性. 1.async:可选表示应该立即下载 ...
- JavaScript之面向对象学习五(JS原生引用类型Array、Object、String等等)的原型对象介绍
1.原型模式的重要性不仅仅体现在创建自定义类型方面,就连所有的原生的引用类型(Obejct.Array.String等等)都在构造函数的原型上定义方法和属性.如下代码可以证明: alert(typeo ...
- 如何调试框架中的app
1,在编写的app中添加断点,并重新生成或编译 2,找到框架app的相应位置代开文件把所用到的dll重新替换成上步生成的dll(bin->debug) 3,运行框架,在VS打开调试->附加 ...