74 lines
2.2 KiB
C#
74 lines
2.2 KiB
C#
using Microsoft.Extensions.Configuration.Json;
|
|
|
|
namespace dy.net
|
|
{
|
|
/// <summary>
|
|
/// appsettings.json操作类
|
|
/// </summary>
|
|
public class Appsettings
|
|
{
|
|
public static IConfiguration Configuration { get; private set; }
|
|
|
|
public Appsettings(string contentPath)
|
|
{
|
|
//如果你把配置文件 是 根据环境变量来分开了,可以这样写
|
|
string Path = $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json";
|
|
|
|
Configuration = new ConfigurationBuilder()
|
|
.SetBasePath(contentPath)
|
|
.Add(new JsonConfigurationSource { Path = Path, Optional = false, ReloadOnChange = true })//这样的话,可以直接读目录里的json文件,而不是 bin 文件夹下的,所以不用修改复制属性
|
|
.Build();
|
|
}
|
|
|
|
public Appsettings(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 封装要操作的字符
|
|
/// </summary>
|
|
/// <param name="sections">节点配置</param>
|
|
/// <returns></returns>
|
|
public static string Get(params string[] sections)
|
|
{
|
|
try
|
|
{
|
|
|
|
if (sections.Any())
|
|
{
|
|
return Configuration[string.Join(":", sections)];
|
|
}
|
|
}
|
|
catch (Exception) { throw; }
|
|
|
|
return "";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 递归获取配置信息数组
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="sections"></param>
|
|
/// <returns></returns>
|
|
public static List<T> Get<T>(params string[] sections)
|
|
{
|
|
List<T> list = new List<T>();
|
|
// 引用 Microsoft.Extensions.Configuration.Binder 包
|
|
Configuration.Bind(string.Join(":", sections), list);
|
|
return list;
|
|
}
|
|
|
|
|
|
public static T Get<T>(string key) where T : new()
|
|
{
|
|
return Configuration.GetSection(key).Get<T>();
|
|
|
|
}
|
|
public static string Get(string Key)
|
|
{
|
|
return Configuration[Key];
|
|
}
|
|
}
|
|
}
|