.Net App.Config 读取

打印 上一主题 下一主题

主题 571|帖子 571|积分 1715

经常能在.Net 项目中看到App.Config/Web.Config ,  一直没有了解过.Net 自带的对配置文件的读写操作,常规的操作类在 System.Configuration.dll 中 ,比较重要的类为ConfigurationManager
底部有全部代码+单元测试
AppSetting 及 ConnectionString 的读取
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3.   <configSections>
  4.     <sectionGroup name="MyGroup" type="AppConfig.MyGroup,Appconfig">
  5.       <section name="MySection" type="AppConfig.MySection,Appconfig" />
  6.     </sectionGroup>
  7.     <section name="MySection" type="AppConfig.MySection,Appconfig" />
  8.   </configSections>
  9.   <appSettings>
  10.     <add key="A" value="a"/>
  11.     <add key="B" value="b"/>
  12.     <add key="C" value="c"/>
  13.   </appSettings>
  14.   <connectionStrings>
  15.     <add connectionString="123" name="1"/>
  16.     <add connectionString="456" name="2"/>
  17.   </connectionStrings>
  18.   <MySection Code="asdas">
  19.     <Member Id="dasd"/>
  20.     <Members>
  21.       <add Id="1"/>
  22.       <add Id="2"/>
  23.       <add Id="3"/>
  24.     </Members>
  25.   </MySection>
  26.   <MyGroup>
  27.     <MySection Code="asdas1">
  28.       <Member Id="dasd1"/>
  29.       <Members>
  30.         <add Id="12"/>
  31.         <add Id="22"/>
  32.       </Members>
  33.     </MySection>
  34.   </MyGroup>
  35. </configuration>
复制代码
指定AppSetting 的 读取
  1. public string GetAppSettingValue(string appSettingName)
  2. {
  3.     return ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(item => item.Equals(appSettingName)) != null ? ConfigurationManager.AppSettings[appSettingName] : null;
  4. }
复制代码
指定ConnecString的获取
  1. public string GetConnectString(string name)
  2. {
  3.     return ConfigurationManager.ConnectionStrings[name]?.ConnectionString;
  4. }
复制代码
自定义内容的读取


  • ConfigurationSection
  • ConfigurationElement
  • ConfigurationSectionGroup
  • ConfigurationElementCollection
ConfigurationSection

表示配置文件中的节。
  1. // 自定义一个Section   
  2. internal class MySection : ConfigurationSection
  3. {
  4.     [ConfigurationProperty(nameof(Code))]
  5.     public string Code
  6.     {
  7.         get => base[nameof(Code)].ToString();
  8.     }
  9. }
复制代码
需要在属性上使用ConfigurationPropertyAttribute
  1. //调用方法      
  2. public string GetMySectionCode()
  3. {
  4.     return (ConfigurationManager.GetSection("MySection") as MySection)?.Code;
  5. }
复制代码
ConfigurationElement

表示Section的成员
  1. //根据Config 配置属性
  2. public class MyElement : ConfigurationElement
  3. {
  4.     [ConfigurationProperty(nameof(Id))]
  5.     public string Id
  6.     {
  7.         get => this[nameof(Id)].ToString();
  8.     }
  9. }
  10. //同时在MySection 中进行扩展
  11. internal class MySection : ConfigurationSection
  12. {
  13.     ...
  14.     [ConfigurationProperty(nameof(Member))]
  15.     public MyElement Member
  16.     {
  17.         get => base[nameof(Member)] as MyElement;
  18.     }
  19.     ...
  20. }
  21. //调用方法
  22. public string GetMySectionMember()
  23. {
  24.     return (ConfigurationManager.GetSection("MySection") as MySection)?.Member?.Id;
  25. }
复制代码
ConfigurationElementCollection

表示Element的集合
  1. //定义一个MyElement 的集合   
  2. [ConfigurationCollection(typeof(MyElement))]
  3. internal class MyElementCollection : ConfigurationElementCollection
  4. {
  5.     protected override ConfigurationElement CreateNewElement()
  6.     {
  7.         return new MyElement();
  8.     }
  9.     protected override object GetElementKey(ConfigurationElement element)
  10.     {
  11.         return (element as MyElement).Id;
  12.     }
  13. }
  14. //在Section 中进行填充
  15. internal class MySection : ConfigurationSection
  16. {
  17.     [ConfigurationProperty(nameof(Members)), ConfigurationCollection(typeof(MyElement))]
  18.     public MyElementCollection Members
  19.     {
  20.         get => base[nameof(Members)] as MyElementCollection;
  21.     }
  22. }
  23. //调用
  24. public int GetMySectionMembers()
  25. {
  26.     return (int)(ConfigurationManager.GetSection("MySection") as MySection)?.Members?.Count;
  27. }
复制代码
ConfigurationSectionGroup

在Section 外面在套一个类
  1. public class MyGroup : ConfigurationSectionGroup
  2. {
  3.     [ConfigurationProperty(nameof(MySection))]
  4.     public MySection MySection { get => this.Sections[nameof(MySection)] as MySection; }
  5. }
复制代码
  1. // 两种调用方式都可以   
  2. public int GetMySectionMembersInGroup()
  3. {
  4.     return (int)(ConfigurationManager.GetSection("MyGroup/MySection") as MySection)?.Members?.Count;
  5. }
  6. public int GetMySectionMembersInGroupByOpenConfig()
  7. {
  8.     Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  9.     var group = (MyGroup)config.GetSectionGroup("MyGroup");
  10.     return group.MySection.Members.Count;
  11. }
复制代码
处理不是默认名字的Config 文件
  1. ExeConfigurationFileMap exeConfigurationFileMap = new ExeConfigurationFileMap() { ExeConfigFilename = @".\App1.config" };
  2. Configuration config = ConfigurationManager.OpenMappedExeConfiguration(exeConfigurationFileMap,ConfigurationUserLevel.None);
复制代码
这边加载方式与上面的不同 后续一致
github

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

刘俊凯

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表