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(); }
时间: 2024-12-13 22:11:09