马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
经常能在.Net 项目中看到App.Config/Web.Config , 一直没有了解过.Net 自带的对配置文件的读写操作,常规的操作类在 System.Configuration.dll 中 ,比较重要的类为ConfigurationManager
底部有全部代码+单元测试
AppSetting 及 ConnectionString 的读取
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <configSections>
- <sectionGroup name="MyGroup" type="AppConfig.MyGroup,Appconfig">
- <section name="MySection" type="AppConfig.MySection,Appconfig" />
- </sectionGroup>
- <section name="MySection" type="AppConfig.MySection,Appconfig" />
- </configSections>
- <appSettings>
- <add key="A" value="a"/>
- <add key="B" value="b"/>
- <add key="C" value="c"/>
- </appSettings>
- <connectionStrings>
- <add connectionString="123" name="1"/>
- <add connectionString="456" name="2"/>
- </connectionStrings>
- <MySection Code="asdas">
- <Member Id="dasd"/>
- <Members>
- <add Id="1"/>
- <add Id="2"/>
- <add Id="3"/>
- </Members>
- </MySection>
- <MyGroup>
- <MySection Code="asdas1">
- <Member Id="dasd1"/>
- <Members>
- <add Id="12"/>
- <add Id="22"/>
- </Members>
- </MySection>
- </MyGroup>
- </configuration>
复制代码 指定AppSetting 的 读取- public string GetAppSettingValue(string appSettingName)
- {
- return ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(item => item.Equals(appSettingName)) != null ? ConfigurationManager.AppSettings[appSettingName] : null;
- }
复制代码 指定ConnecString的获取- public string GetConnectString(string name)
- {
- return ConfigurationManager.ConnectionStrings[name]?.ConnectionString;
- }
复制代码 自定义内容的读取
- ConfigurationSection
- ConfigurationElement
- ConfigurationSectionGroup
- ConfigurationElementCollection
ConfigurationSection
表示配置文件中的节。
- // 自定义一个Section
- internal class MySection : ConfigurationSection
- {
- [ConfigurationProperty(nameof(Code))]
- public string Code
- {
- get => base[nameof(Code)].ToString();
- }
- }
复制代码 需要在属性上使用ConfigurationPropertyAttribute- //调用方法
- public string GetMySectionCode()
- {
- return (ConfigurationManager.GetSection("MySection") as MySection)?.Code;
- }
复制代码 ConfigurationElement
表示Section的成员
- //根据Config 配置属性
- public class MyElement : ConfigurationElement
- {
- [ConfigurationProperty(nameof(Id))]
- public string Id
- {
- get => this[nameof(Id)].ToString();
- }
- }
- //同时在MySection 中进行扩展
- internal class MySection : ConfigurationSection
- {
- ...
- [ConfigurationProperty(nameof(Member))]
- public MyElement Member
- {
- get => base[nameof(Member)] as MyElement;
- }
- ...
- }
- //调用方法
- public string GetMySectionMember()
- {
- return (ConfigurationManager.GetSection("MySection") as MySection)?.Member?.Id;
- }
复制代码 ConfigurationElementCollection
表示Element的集合
- //定义一个MyElement 的集合
- [ConfigurationCollection(typeof(MyElement))]
- internal class MyElementCollection : ConfigurationElementCollection
- {
- protected override ConfigurationElement CreateNewElement()
- {
- return new MyElement();
- }
- protected override object GetElementKey(ConfigurationElement element)
- {
- return (element as MyElement).Id;
- }
- }
- //在Section 中进行填充
- internal class MySection : ConfigurationSection
- {
- [ConfigurationProperty(nameof(Members)), ConfigurationCollection(typeof(MyElement))]
- public MyElementCollection Members
- {
- get => base[nameof(Members)] as MyElementCollection;
- }
- }
- //调用
- public int GetMySectionMembers()
- {
- return (int)(ConfigurationManager.GetSection("MySection") as MySection)?.Members?.Count;
- }
复制代码 ConfigurationSectionGroup
在Section 外面在套一个类
- public class MyGroup : ConfigurationSectionGroup
- {
- [ConfigurationProperty(nameof(MySection))]
- public MySection MySection { get => this.Sections[nameof(MySection)] as MySection; }
- }
复制代码- // 两种调用方式都可以
- public int GetMySectionMembersInGroup()
- {
- return (int)(ConfigurationManager.GetSection("MyGroup/MySection") as MySection)?.Members?.Count;
- }
- public int GetMySectionMembersInGroupByOpenConfig()
- {
- Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- var group = (MyGroup)config.GetSectionGroup("MyGroup");
- return group.MySection.Members.Count;
- }
复制代码 处理不是默认名字的Config 文件
- ExeConfigurationFileMap exeConfigurationFileMap = new ExeConfigurationFileMap() { ExeConfigFilename = @".\App1.config" };
- Configuration config = ConfigurationManager.OpenMappedExeConfiguration(exeConfigurationFileMap,ConfigurationUserLevel.None);
复制代码 这边加载方式与上面的不同 后续一致
github
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |