刘俊凯 发表于 2022-12-15 07:23:04

.Net App.Config 读取

经常能在.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 : null;
}指定ConnecString的获取
public string GetConnectString(string name)
{
    return ConfigurationManager.ConnectionStrings?.ConnectionString;
}自定义内容的读取


[*]ConfigurationSection
[*]ConfigurationElement
[*]ConfigurationSectionGroup
[*]ConfigurationElementCollection
ConfigurationSection

表示配置文件中的节。
// 自定义一个Section   
internal class MySection : ConfigurationSection
{
   
    public string Code
    {
      get => base.ToString();
    }
}需要在属性上使用ConfigurationPropertyAttribute
//调用方法      
public string GetMySectionCode()
{
    return (ConfigurationManager.GetSection("MySection") as MySection)?.Code;
}ConfigurationElement

表示Section的成员
//根据Config 配置属性
public class MyElement : ConfigurationElement
{
   
    public string Id
    {
      get => this.ToString();
    }
}
//同时在MySection 中进行扩展
internal class MySection : ConfigurationSection
{
    ...
   
    public MyElement Member
    {
      get => base as MyElement;
    }
    ...
}
//调用方法
public string GetMySectionMember()
{
    return (ConfigurationManager.GetSection("MySection") as MySection)?.Member?.Id;
}ConfigurationElementCollection

表示Element的集合
//定义一个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
{
   
    public MyElementCollection Members
    {
      get => base as MyElementCollection;
    }
}
//调用
public int GetMySectionMembers()
{
    return (int)(ConfigurationManager.GetSection("MySection") as MySection)?.Members?.Count;
}ConfigurationSectionGroup

在Section 外面在套一个类
public class MyGroup : ConfigurationSectionGroup
{
   
    public MySection MySection { get => this.Sections 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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: .Net App.Config 读取