配置文件加载

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Configuration;
  4 using System.IO;
  5 using System.Linq;
  6 using System.Reflection;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9
 10 namespace Test
 11 {
 12     public abstract class BaseElementCollection : ConfigurationElementCollection
 13     {
 14         public override ConfigurationElementCollectionType CollectionType
 15         {
 16             get
 17             {
 18                 return ConfigurationElementCollectionType.AddRemoveClearMap;
 19             }
 20         }
 21
 22         public new string AddElementName
 23         {
 24             get { return base.AddElementName; }
 25             set { base.AddElementName = value; }
 26         }
 27
 28         public new string ClearElementName
 29         {
 30             get { return base.ClearElementName; }
 31             set { base.ClearElementName = value; }
 32         }
 33
 34         public new string RemoveElementName
 35         {
 36             get
 37             {
 38                 return base.RemoveElementName;
 39             }
 40         }
 41
 42         public new int Count
 43         {
 44             get { return base.Count; }
 45         }
 46
 47         protected override void BaseAdd(ConfigurationElement element)
 48         {
 49             base.BaseAdd(element);
 50         }
 51
 52         public void Clear()
 53         {
 54             BaseClear();
 55         }
 56     }
 57
 58     public class ResearchOrganizationElement : ConfigurationElement
 59     {
 60         public ResearchOrganizationElement()
 61         {
 62
 63         }
 64
 65
 66         public ResearchOrganizationElement(string name)
 67         {
 68             Name = name;
 69         }
 70
 71         [ConfigurationProperty("name", IsRequired = true, IsKey = false)]
 72         public string Name
 73         {
 74             get { return (string)this["name"]; }
 75             set { this["name"] = value; }
 76         }
 77
 78         [ConfigurationProperty("companyId", IsRequired = true, IsKey = false)]
 79         public string CompanyId
 80         {
 81             get { return (string)this["companyId"]; }
 82             set { this["companyId"] = value; }
 83         }
 84     }
 85
 86     public class ResearchOrganizationCollection : BaseElementCollection
 87     {
 88         public ResearchOrganizationCollection()
 89         {
 90
 91         }
 92
 93         protected override
 94             ConfigurationElement CreateNewElement()
 95         {
 96             return new ResearchOrganizationElement();
 97         }
 98
 99         protected override
100            ConfigurationElement CreateNewElement(string elementName)
101         {
102             return new ResearchOrganizationElement(elementName);
103         }
104
105         protected override Object
106            GetElementKey(ConfigurationElement element)
107         {
108             return ((ResearchOrganizationElement)element).Name;
109         }
110
111         protected ResearchOrganizationElement this[int index]
112         {
113             get { return (ResearchOrganizationElement)BaseGet(index); }
114             set
115             {
116                 if (BaseGet(index) != null)
117                 {
118                     BaseRemoveAt(index);
119                 }
120                 BaseAdd(index, value);
121             }
122         }
123
124         public new ResearchOrganizationElement this[string name]
125         {
126             get
127             {
128                 String Key = name.ToLower();
129                 return (ResearchOrganizationElement)BaseGet(name);
130             }
131         }
132
133         public int IndexOf(ResearchOrganizationElement element)
134         {
135             return BaseIndexOf(element);
136         }
137     }
138
139     public class ReseachOrganizationSection : ConfigurationSection
140     {
141         ResearchOrganizationElement element;
142         public ReseachOrganizationSection()
143         {
144             element = new ResearchOrganizationElement();
145         }
146
147         [ConfigurationProperty("researchOrganizations",IsDefaultCollection=false)]
148         public ResearchOrganizationCollection ResearchOrganizations
149         {
150             get
151             {
152                 ResearchOrganizationCollection collection = (ResearchOrganizationCollection)base["researchOrganizations"];
153                 return collection;
154             }
155         }
156     }
157
158     public class ConfigDemocs
159     {
160         public static readonly ConfigDemocs Current = new ConfigDemocs();
161
162         private ConfigDemocs()
163         {
164             InitConfiguration();
165         }
166
167         private Configuration config;
168
169         private ReseachOrganizationSection researchOragnizationSection;
170
171         public ReseachOrganizationSection ReseachOrganizationSection
172         {
173             get
174             {
175                 return researchOragnizationSection;
176             }
177         }
178
179         public void InitConfiguration()
180         {
181             ConfigurationSection section;
182
183             ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
184             configFile.ExeConfigFilename = Path.GetFileName(Assembly.GetEntryAssembly().Location + ".config");
185             config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);
186
187             section = config.Sections["ReseachOrganizationSection"];
188             if (section == null)
189             {
190                 researchOragnizationSection = new ReseachOrganizationSection();
191                 config.Sections.Add("ReseachOrganizationSection", researchOragnizationSection);
192             }
193             else
194             {
195                 researchOragnizationSection = (ReseachOrganizationSection)section;
196             }
197             researchOragnizationSection.SectionInformation.ForceSave = true;
198         }
199     }
200 }
 1 //调用
 2         static void Main(string[] args)
 3         {
 4             Console.WriteLine("******ReseachOrganizationSection******");
 5             ResearchOrganizationElement element = ConfigDemocs.Current.ReseachOrganizationSection.ResearchOrganizations["中信证券"];
 6
 7             Console.WriteLine(element.Name + "  " + element.CompanyId);
 8
 9             Console.ReadLine();
10         }
 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3   <configSections>
 4     <section name="ReseachOrganizationSection" type="Test.ReseachOrganizationSection, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
 5   </configSections>
 6
 7   <ReseachOrganizationSection>
 8     <researchOrganizations>
 9       <clear/>
10       <add name="天风证券" companyId="001"></add>
11       <add name="中信证券" companyId="002"></add>
12     </researchOrganizations>
13   </ReseachOrganizationSection>
14   <startup>
15     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
16   </startup>
17 </configuration>
时间: 2024-10-10 01:42:06

配置文件加载的相关文章

关于bash的配置文件加载

bash的配置文件分为两类全局配置 /etc/profile  /etc/profile.d/*.sh  /etc/bashrc个人配置 ~/.bash_profile  ~/.bashrc profile类的配置:设定环境变量:运行命令或脚本bashrc类的配置:设定本地变量:定义命令别名 登录式shell如何读取配置文件 (通过本地命令行或远程终端登录:su - username)/etc/profile --> /etc/profile.d/*.sh --> ~/.bash_profil

hibernate源码-配置文件加载过程分析

Hibernate建议,在一个应用系统当中Configuration与SessionFactory为单例,Session为多例. 当我们执行如下代码,hibernate开始加载默认的配置文件 new Configuration().configure() hibernate会在classath的根路径下,查找名为"hibernate.cfg.xml" 的配置文件,并解析它,过程如图1所示 图1:配置文件加载过程时序图 下面一起分析一下配置文件加载过程 Step 1.Configurat

saltstack源码-启动3-config.py配置文件加载

#目标文件位置/usr/lib/python2.6/site-packages/salt/config.py#这个文件加载配置文件的模块.master和minion的配置文件加载都是在这个模块里面完成的#master的启动在这模块里面只涉及到方法和属性只有几个 master和minion的默认配置属性也在这个文件里面定义 DEFAULT_MASTER_OPTS = { 'interface': '0.0.0.0', 'publish_port': '4505', 'pub_hwm': 1000,

.NET Core配置文件加载与DI注入配置数据

.NET Core配置文件 在以前.NET中配置文件都是以App.config / Web.config等XML格式的配置文件,而.NET Core中建议使用以JSON为格式的配置文件,因为使用起来更加方面灵活,而且可以使用.NET Core中的DI注入配置数据. 使用: 1 var config = new ConfigurationBuilder() 2 .AddInMemoryCollection() //将配置文件的数据加载到内存中 3 .SetBasePath(Directory.Ge

struts2中配置文件加载的顺序是什么?

struts2的StrutsPrepareAndExecuteFilter拦截器中对Dispatcher进行了初始化 在Dispatcher类的init方法中定义了配置文件的加载顺序(下面是源码) public void init() { if (configurationManager == null) { configurationManager = createConfigurationManager(DefaultBeanSelectionProvider.DEFAULT_BEAN_NA

Mybatis 源码分析--Configuration.xml配置文件加载到内存

(补充知识点: 1 byte(字节)=8 bit(位) 通常一个标准英文字母占一个字节位置,一个标准汉字占两个字节位置:字符的例子有:字母.数字系统或标点符号) 1.创建SqlSessionFactory ①Reader reader = Resources.getResourceAsReader("mybatis-config.xml");                       //获取mybatis配置文件的字符 注解:Resources类是在mybatis中定义的一个类:g

Spring的多配置文件加载

如果配置文件存在多个的情况下,加载配置文件的方式是:1--可以指定总的配置文件去包含子的配置文件,然后只加载总的配置文件即可 在总配置文件applicationContext.xml 中引入子文件 <import resource="applicationContext-action.xml"/> <import resource="applicationContext-dao.xml"/> ApplicationContext ac = n

hadoop配置文件加载顺序

用了一段时间的hadoop,现在回来看看源码发现别有一番味道,温故而知新,还真是这样的 在使用hadoop之前我们需要配置一些文件,hadoop-env.sh,core-site.xml,hdfs-site.xml,mapred-site.xml.那么这些文件在什么时候被hadoop使用? 一般的在启动hadoop的时候使用最多就是start-all.sh,那么这个脚本都干了些什么? start-all.sh # Start all hadoop daemons. Run this on mas

Python 配置文件加载且自动更新(watchdog)

安装依赖:pip install watchdog #!/usr/bin/env python3 # -*- coding: utf-8 -*- import logging import os import threading from configparser import ConfigParser, NoOptionError from watchdog.events import FileSystemEventHandler from watchdog.observers import

nginx中有关命令和日志切割,配置文件加载的详细阐述

一.Nginx简介 Nginx ("engine x") 是俄罗斯人Igor Sysoev(塞索耶夫)编写的一款高性能的 HTTP 和反向代理服务器.Nginx 已经在俄罗斯最大的门户网站── Rambler Media(www.rambler.ru)上运行了4年时间,同时俄罗斯超过20%的虚拟主机平台采用Nginx作为反向代理服务器.在国内,已经有新 浪博客.新浪播客.搜狐通行证.网易新闻.网易博客.金山逍遥网.金山爱词霸.校内网.YUPOO相册.豆瓣.迅雷看看等多家网站.频道使用