using System;
using System.IO;
using Microsoft.Extensions.Configuration;
namespace Admin.Core.Common.Helpers
{
///
/// 配置帮助类
///
public class ConfigHelper
{
///
/// 加载配置文件
///
/// 文件名称
/// 环境名称
/// 自动更新
///
public IConfiguration Load(string fileName, string environmentName = "", bool reloadOnChange = false)
{
var filePath = Path.Combine(AppContext.BaseDirectory, "configs");
if (!Directory.Exists(filePath))
return null;
var builder = new ConfigurationBuilder()
.SetBasePath(filePath)
.AddJsonFile(fileName.ToLower() + ".json", true, reloadOnChange);
if (environmentName.NotNull())
{
builder.AddJsonFile(fileName.ToLower() + "." + environmentName + ".json", true, reloadOnChange);
}
return builder.Build();
}
///
/// 获得配置信息
///
/// 配置信息
///
/// 文件名称
/// 自动更新
///
public T Get(string fileName, string environmentName = "", bool reloadOnChange = false)
{
var configuration = Load(fileName, environmentName, reloadOnChange);
if (configuration == null)
return default;
return configuration.Get();
}
}
}