首页 > 代码库 > 自定义ConfigurationManager
自定义ConfigurationManager
在C#里面,读取App.config或者Web.config里面的配置信息很容易,但是有时候我们想把配置信息独立到另一个文件里面,这种情况的实现就很少看到了,网上的资料也比较少,而且大部分都是转载,抄过来运行效果也不好。今天整理了一下,记录下来,方便以后用。
其中过程不复杂,主要重写ConfigurationSection,ConfigurationElementCollection,ConfigurationElement这三个类就可以。
1.ConfigurationSection
1 public class BooksSection : ConfigurationSection 2 { 3 [ConfigurationProperty("books", IsRequired = true)] 4 public string Category 5 { 6 7 get 8 { 9 return (string)base["Category"];10 }11 12 set13 {14 base["Category"] = value;15 }16 17 }18 [ConfigurationProperty("", IsDefaultCollection = true)]19 public BookElementCollection Books20 {21 22 get23 {24 return (BookElementCollection)base[""];25 }26 27 }28 }
2.ConfigurationElementCollection
1 public class BookElementCollection : ConfigurationElementCollection 2 { 3 protected override ConfigurationElement CreateNewElement() 4 { 5 return new BookElement(); 6 } 7 8 protected override object GetElementKey(ConfigurationElement element) 9 {10 return ((BookElement)element).Name;11 }12 13 public override ConfigurationElementCollectionType CollectionType14 {15 get16 {17 return ConfigurationElementCollectionType.BasicMap;18 }19 }20 21 protected override string ElementName22 {23 get24 {25 return "book";26 }27 }28 public BookElement this[int index]29 {30 31 get32 {33 return (BookElement)BaseGet(index);34 }35 set36 {37 if (BaseGet(index) != null)38 {39 BaseRemoveAt(index);40 }41 BaseAdd(index, value);42 }43 44 }45 }
3.ConfigurationElement
1 public class BookElement : ConfigurationElement 2 { 3 4 [ConfigurationProperty("name", IsRequired = true)] 5 public string Name 6 { 7 get 8 { 9 return (string)base["name"];10 }11 12 set13 {14 base["name"] = value;15 }16 17 }18 19 [ConfigurationProperty("author", IsRequired = true)]20 21 public double Author22 {23 get24 {25 return (double)base["author"];26 }27 28 set29 {30 base["author"] = value;31 }32 }33 34 }
4.config文件
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <configSections> 4 <section name="books" type="ConfigurationDemo.BooksSection, ConfigurationDemo"/> 5 </configSections> 6 <books> 7 <book name="123" author="456"/> 8 </books> 9 <appSettings>10 <add key="name" value="123"/>11 </appSettings>12 </configuration>
5.测试代码
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 string configPath = @"E:\Projects\ConfigurationDemo\ConfigurationDemo\bin\Debug\App.config"; 6 ExeConfigurationFileMap map = new ExeConfigurationFileMap(); 7 map.ExeConfigFilename = configPath; 8 9 var configManager = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);10 if (configManager.HasFile)11 {12 13 BooksSection config = (BooksSection)configManager.GetSection("books");14 Console.WriteLine(config.Books[0].Name);15 Console.WriteLine(configManager.AppSettings.Settings["name"].Value);16 }17 }18 }
6.测试结果
123
123
请按任意键继续. . .
自定义ConfigurationManager
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。