经常能在.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);
这边加载方式与上面的不同 后续一致
标签:nameof,string,ConfigurationManager,App,return,Net,Config,public,MySection From: https://www.cnblogs.com/dongyaosheng/p/16982434.html