码迷,mamicode.com
首页 > 其他好文 > 详细

配置文件加载

时间:2017-08-22 01:37:47      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:main   text   art   conf   sde   exec   cat   pac   .section   

  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>

 

 

配置文件加载

标签:main   text   art   conf   sde   exec   cat   pac   .section   

原文地址:http://www.cnblogs.com/qgqcnblogs/p/7407309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!