Create a custom configSection in web.config or app.config file

來源:互聯網
上載者:User

標籤:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.