2021-10-12 16:52:28 +08:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
using Microsoft.Extensions.Configuration.Json;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Yi.Framework.WebCore
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// appsettings.json操作类
|
|
|
|
|
|
/// </summary>
|
2021-10-26 15:09:07 +08:00
|
|
|
|
public class Appsettings
|
2021-10-12 16:52:28 +08:00
|
|
|
|
{
|
2022-10-17 18:08:16 +08:00
|
|
|
|
static IConfiguration? Configuration { get; set; }
|
|
|
|
|
|
static string? contentPath { get; set; }
|
2022-10-14 18:08:45 +08:00
|
|
|
|
|
2021-10-12 16:52:28 +08:00
|
|
|
|
public Appsettings(string contentPath)
|
|
|
|
|
|
{
|
|
|
|
|
|
string Path = "appsettings.json";
|
|
|
|
|
|
|
|
|
|
|
|
//如果你把配置文件 是 根据环境变量来分开了,可以这样写
|
|
|
|
|
|
//Path = $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json";
|
|
|
|
|
|
|
|
|
|
|
|
Configuration = new ConfigurationBuilder()
|
|
|
|
|
|
.SetBasePath(contentPath)
|
|
|
|
|
|
.Add(new JsonConfigurationSource { Path = Path, Optional = false, ReloadOnChange = true })//这样的话,可以直接读目录里的json文件,而不是 bin 文件夹下的,所以不用修改复制属性
|
|
|
|
|
|
.Build();
|
|
|
|
|
|
}
|
2021-10-26 15:09:07 +08:00
|
|
|
|
|
2021-10-12 16:52:28 +08:00
|
|
|
|
public Appsettings(IConfiguration configuration)
|
|
|
|
|
|
{
|
|
|
|
|
|
Configuration = configuration;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 封装要操作的字符
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sections">节点配置</param>
|
|
|
|
|
|
/// <returns></returns>
|
2022-10-17 18:08:16 +08:00
|
|
|
|
public static string? app(params string[] sections)
|
2021-10-12 16:52:28 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
if (sections.Any())
|
|
|
|
|
|
{
|
2022-10-17 18:08:16 +08:00
|
|
|
|
return Configuration?[string.Join(":", sections)];
|
2021-10-12 16:52:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception) { }
|
|
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-26 15:09:07 +08:00
|
|
|
|
public static bool appBool(params string[] sections)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
return Bool(app(sections));
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-17 18:08:16 +08:00
|
|
|
|
public static bool Bool(object? thisValue)
|
2021-10-26 15:09:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
bool reval = false;
|
|
|
|
|
|
if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
|
|
|
|
|
|
{
|
|
|
|
|
|
return reval;
|
|
|
|
|
|
}
|
|
|
|
|
|
return reval;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-12 16:52:28 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 递归获取配置信息数组
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="sections"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static T app<T>(params string[] sections)
|
|
|
|
|
|
{
|
2021-10-13 15:40:56 +08:00
|
|
|
|
T app = Activator.CreateInstance<T>();
|
2021-10-12 16:52:28 +08:00
|
|
|
|
// 引用 Microsoft.Extensions.Configuration.Binder 包
|
2021-10-13 15:40:56 +08:00
|
|
|
|
Configuration.Bind(string.Join(":", sections), app);
|
|
|
|
|
|
return app;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-10-18 09:01:16 +08:00
|
|
|
|
public static IConfiguration? appConfiguration(params string[] sections)
|
2021-10-13 15:40:56 +08:00
|
|
|
|
{
|
2022-10-18 09:01:16 +08:00
|
|
|
|
return Configuration?.GetSection(string.Join(":", sections));
|
2021-10-12 16:52:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|