zoukankan      html  css  js  c++  java
  • .net分布在指定文件夹的web.confgi或者app.config

    .Net里面,ConfigurationManager默认读取的是Web.config或者App.config但是,什么都放在这两个文件里面,感觉太多了,也不好管理配置。于是参考了下别人的资料,自己写了一个例子,例子实现的的是E:App.config的文件,文件格式如下
    
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="books" type="ConfigurationDemo.BooksSection, ConfigurationDemo"/>
      </configSections>
      <books>
        <book name="123" author="456"/>
      </books>  
    </configuration>
    
    要实现这样的功能,就要实现自己的ConfigurationSection,ConfigurationElementCollection,ConfigurationElement 
    
    
    1.ConfigurationSection
    
    public class BooksSection : ConfigurationSection
        {
            [ConfigurationProperty("books", IsRequired = true)]
            public string Category
            {
    
                get
                {
                    return (string)base["Category"];
                }
    
                set
                {
                    base["Category"] = value;
                }
    
            }
            [ConfigurationProperty("", IsDefaultCollection = true)]
            public BookElementCollection Books
            {
    
                get
                {
                    return (BookElementCollection)base[""];
                }
    
            }
    
        }
    
    2.ConfigurationElementCollection
    
        public class BookElementCollection : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new BookElement();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((BookElement)element).Name;
            }
    
            public override ConfigurationElementCollectionType CollectionType
            {
                get
                {
                    return ConfigurationElementCollectionType.BasicMap;
                }
            }
    
            protected override string ElementName
            {
                get
                {
                    return "book";
                }
            }
            public BookElement this[int index]
            {
    
                get
                {
                    return (BookElement)BaseGet(index);
                }
                set
                {
                    if (BaseGet(index) != null)
                    {
                        BaseRemoveAt(index);
                    }
                    BaseAdd(index, value);
                }
    
            }
        }
    
    
    3.ConfigurationElement
    
        public class BookElement : ConfigurationElement
        {
    
            [ConfigurationProperty("name", IsRequired = true)]
            public string Name
            {
                get
                {
                    return (string)base["name"];
                }
    
                set
                {
                    base["name"] = value;
                }
    
            }
    
            [ConfigurationProperty("author", IsRequired = true)]
    
            public double Author
            {
                get
                {
                    return (double)base["author"];
                }
    
                set
                {
                    base["author"] = value;
                }
            }
    
        }
    
    
    
    4.0Config文件
    
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="books" type="ConfigurationDemo.BooksSection, ConfigurationDemo"/>
      </configSections>
      <books>
        <book name="123" author="456"/>
      </books>  
    </configuration>
    
    5.0读取
    
    
            static void Main(string[] args)
            {
                string configPath = @"E:App.config";
                ExeConfigurationFileMap map = new ExeConfigurationFileMap();
                map.ExeConfigFilename = configPath;
    
                var configManager = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
                if (configManager.HasFile)
                {
                    BooksSection config = (BooksSection)configManager.GetSection("books");
                    Console.WriteLine(config.Books[0].Name);
                }
    
            }
    结果
    
    123
    请按任意键继续. . .
  • 相关阅读:
    Excel宏打开URL 特殊字符#变了
    windows升级到1607后操作很卡顿的解决办法
    打包解决方案后,安装时提示只能在IIS5.1以上运行解决方法
    在CS代码页获取input输入框内肉----.net学习点滴
    greenplum 6.7编译安装插件pxf
    greenplum 6.7编译安装插件mysql_fdw
    python3 + greenplum-spark-connector访问greenplum
    centos7无cm安装hadoop+spark
    hive实现update和delete功能
    使得hivecli访问hive
  • 原文地址:https://www.cnblogs.com/kexb/p/6880067.html
Copyright © 2011-2022 走看看