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

Source: Internet
Author: User

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

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();        }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.