程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> Create a custom configSection in web.config or app.config file,web.configsection

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

編輯:C#入門知識

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved