You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.2 KiB
C#

using Microsoft.Extensions.Configuration;
namespace RoBot.Core.Helper
{
public class AppConfigHelper
{
private static readonly IConfiguration _configuration;
static AppConfigHelper()
{
_configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory) // 控制台运行目录
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
}
/// <summary>
/// 获取字符串配置值
/// </summary>
public static string Get(string key)
{
return _configuration[key];
}
/// <summary>
/// 获取强类型配置值
/// </summary>
public static T GetValue<T>(string key)
{
return _configuration.GetValue<T>(key);
}
/// <summary>
/// 获取绑定到类的配置对象(适用于嵌套配置)
/// </summary>
public static T GetSection<T>(string sectionName) where T : new()
{
var obj = new T();
_configuration.GetSection(sectionName).Bind(obj);
return obj;
}
}
}