自定义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             set
13             {
14                 base["Category"] = value;
15             }
16 
17         }
18         [ConfigurationProperty("", IsDefaultCollection = true)]
19         public BookElementCollection Books
20         {
21 
22             get
23             {
24                 return (BookElementCollection)base[""];
25             }
26 
27         }
28     }
View Code

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 CollectionType
14         {
15             get
16             {
17                 return ConfigurationElementCollectionType.BasicMap;
18             }
19         }
20 
21         protected override string ElementName
22         {
23             get
24             {
25                 return "book";
26             }
27         }
28         public BookElement this[int index]
29         {
30 
31             get
32             {
33                 return (BookElement)BaseGet(index);
34             }
35             set
36             {
37                 if (BaseGet(index) != null)
38                 {
39                     BaseRemoveAt(index);
40                 }
41                 BaseAdd(index, value);
42             }
43 
44         }
45     }
View Code

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             set
13             {
14                 base["name"] = value;
15             }
16 
17         }
18 
19         [ConfigurationProperty("author", IsRequired = true)]
20 
21         public double Author
22         {
23             get
24             {
25                 return (double)base["author"];
26             }
27 
28             set
29             {
30                 base["author"] = value;
31             }
32         }
33 
34     }
View Code

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>
View Code

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     }
View Code

6.测试结果

123
123
请按任意键继续. . .

转载于:https://www.cnblogs.com/campanula/p/4142285.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值