首先是添加NuGet包依赖,主要依赖一下3个包
Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.FileExtensions
Microsoft.Extensions.Configuration.Json
然后我们可以写一个工具类,如下:
ConfigurationHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
namespace Wongoing.Basic
{
/// <summary>
/// appsettings配置读取辅助类
/// </summary>
public class ConfigurationHelper
{
#region 字段定义
private static readonly string CONFIGFILELOCK = "CONFIGFILELOCK";
private static readonly string APPSETTINGS = "AppSettings";
private static readonly string PROSETTINGS = "ProSettings";
private static IConfigurationRoot configurationRoot = null;
#endregion
#region 单例实现
private static ConfigurationHelper _instance = null;
private ConfigurationHelper()
{
configurationRoot = new ConfigurationBuilder()
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory()))
.AddJsonFile("appsettings.json", optional: false)
.Build();
}
public static ConfigurationHelper Instance
{
get
{
lock(CONFIGFILELOCK)
{
if (_instance == null)
{
_instance = new ConfigurationHelper();
}
return _instance;
}
}
}
#endregion
#region GetConnectionString
/// <summary>
/// 获取配置文件中ConnectionStrings中某个配置项的数据库连接字符串
/// </summary>
/// <param name="key">ConnectionStrings中某个配置项</param>
/// <param name="defaultValue">如果没有找到对应的配置项,则返回默认值</param>
/// <returns>返回配置文件中ConnectionStrings中某个配置项的数据库连接字符串</returns>
public string GetConnectionString(string key, string defaultValue)
{
string result = configurationRoot.GetConnectionString(key);
if (result == null)
{
return defaultValue;
}
else
{
return result;
}
}
#endregion
#region GetAppSetting
/// <summary>
/// 获取配置文件中AppSettings中某个配置项的值
/// </summary>
/// <param name="key">AppSettings中某个配置项</param>
/// <param name="defaultValue">如果没有找到对应的配置项,则返回默认值</param>
/// <returns>返回配置文件中AppSettings中某个配置项的值</returns>
public string GetAppSetting(string key, string defaultValue)
{
string result = defaultValue;
IConfigurationSection configurationSection = configurationRoot.GetSection(APPSETTINGS);
if (configurationSection != null)
{
if (configurationSection.GetSection(key) != null)
{
result = configurationSection.GetSection(key).Value;
}
}
return result;
}
/// <summary>
/// 获取配置文件中AppSettings中某个配置项的值
/// </summary>
/// <param name="key">AppSettings中某个配置项</param>
/// <param name="defaultValue">如果没有找到对应的配置项,则返回默认值</param>
/// <returns>返回配置文件中AppSettings中某个配置项的值</returns>
public int GetAppSetting(string key, int defaultValue)
{
int result = defaultValue;
IConfigurationSection configurationSection = configurationRoot.GetSection(APPSETTINGS);
if (configurationSection != null)
{
if (configurationSection.GetSection(key) != null)
{
string strResult = configurationSection.GetSection(key).Value;
int.TryParse(strResult, out result);
}
}
return result;
}
/// <summary>
/// 获取配置文件中AppSettings中某个配置项的值
/// </summary>
/// <param name="key">AppSettings中某个配置项</param>
/// <param name="defaultValue">如果没有找到对应的配置项,则返回默认值</param>
/// <returns>返回配置文件中AppSettings中某个配置项的值</returns>
public bool GetAppSetting(string key, bool defaultValue)
{
bool result = defaultValue;
IConfigurationSection configurationSection = configurationRoot.GetSection(APPSETTINGS);
if (configurationSection != null)
{
if (configurationSection.GetSection(key) != null)
{
string strResult = configurationSection.GetSection(key).Value;
bool.TryParse(strResult, out result);
}
}
return result;
}
/// <summary>
/// 获取配置文件中AppSettings中某个配置项的值
/// </summary>
/// <param name="key">AppSettings中某个配置项</param>
/// <param name="defaultValue">如果没有找到对应的配置项,则返回默认值</param>
/// <returns>返回配置文件中AppSettings中某个配置项的值</returns>
public double GetAppSetting(string key, double defaultValue)
{
double result = defaultValue;
IConfigurationSection configurationSection = configurationRoot.GetSection(APPSETTINGS);
if (configurationSection != null)
{
if (configurationSection.GetSection(key) != null)
{
string strResult = configurationSection.GetSection(key).Value;
double.TryParse(strResult, out result);
}
}
return result;
}
#endregion
#region GetProSetting
/// <summary>
/// 获取配置文件中ProSettings中某个配置项的值
/// </summary>
/// <param name="key">ProSettings中某个配置项</param>
/// <param name="defaultValue">如果没有找到对应的配置项,则返回默认值</param>
/// <returns>返回配置文件中ProSettings中某个配置项的值</returns>
public string GetProSetting(string key, string defaultValue)
{
string result = defaultValue;
IConfigurationSection configurationSection = configurationRoot.GetSection(PROSETTINGS);
if (configurationSection != null)
{
if (configurationSection.GetSection(key) != null)
{
result = configurationSection.GetSection(key).Value;
}
}
return result;
}
/// <summary>
/// 获取配置文件中ProSettings中某个配置项的值
/// </summary>
/// <param name="key">ProSettings中某个配置项</param>
/// <param name="defaultValue">如果没有找到对应的配置项,则返回默认值</param>
/// <returns>返回配置文件中ProSettings中某个配置项的值</returns>
public int GetProSetting(string key, int defaultValue)
{
int result = defaultValue;
IConfigurationSection configurationSection = configurationRoot.GetSection(PROSETTINGS);
if (configurationSection != null)
{
if (configurationSection.GetSection(key) != null)
{
string strResult = configurationSection.GetSection(key).Value;
int.TryParse(strResult, out result);
}
}
return result;
}
/// <summary>
/// 获取配置文件中ProSettings中某个配置项的值
/// </summary>
/// <param name="key">ProSettings中某个配置项</param>
/// <param name="defaultValue">如果没有找到对应的配置项,则返回默认值</param>
/// <returns>返回配置文件中ProSettings中某个配置项的值</returns>
public bool GetProSetting(string key, bool defaultValue)
{
bool result = defaultValue;
IConfigurationSection configurationSection = configurationRoot.GetSection(APPSETTINGS);
if (configurationSection != null)
{
if (configurationSection.GetSection(key) != null)
{
string strResult = configurationSection.GetSection(key).Value;
bool.TryParse(strResult, out result);
}
}
return result;
}
#endregion
}
}
appsettings.json内容实例如下:
{
"ConnectionStrings": {
"DefaultConnection": "Server=10.10.80.15;Port=3306;DataBase=wongoing-dcs;User Id=root;Password=root@123;Allow User Variables=True;Connection Timeout=15"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ProSettings": {
"NavTheme": "dark",
"Layout": "side",
"ContentWidth": "Fluid",
"FixedHeader": true,
"FixSiderbar": true,
"Title": "工业数采",
"PrimaryColor": "daybreak",
"ColorWeak": false,
"SplitMenus": false,
"HeaderRender": true,
"FooterRender": true,
"MenuRender": true,
"MenuHeaderRender": true,
"HeaderHeight": 48
}
}
public static readonly string Title = Wongoing.Basic.ConfigurationHelper.Instance.GetProSetting("Title", "工业数采");
/// <summary>
/// 默认数据库连接字符串
/// </summary>
public static readonly string DefaultConnection = Wongoing.Basic.ConfigurationHelper.Instance.GetConnectionString("DefaultConnection",
"Server=localhost;DataBase=wongoing-dcs;User Id=root;Password=wongoing@123;Allow User Variables=True;Connection Timeout=15");
标签:configurationSection,string,配置文件,获取,GetSection,result,key,动态
From: https://www.cnblogs.com/caozhengze/p/16962125.html