66 lines
1.7 KiB
C#
66 lines
1.7 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Configuration.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SA.Core.Init
|
|
{
|
|
public class InitConfiguration
|
|
{
|
|
|
|
static IConfiguration Configuration { get; set; }
|
|
|
|
|
|
|
|
static InitConfiguration()
|
|
{
|
|
string Path = "appsettings.json";
|
|
|
|
//Path = $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json";
|
|
Configuration = new ConfigurationBuilder()
|
|
|
|
.Add(new JsonConfigurationSource { Path = Path, Optional = false, ReloadOnChange = true })
|
|
.Build();
|
|
}
|
|
public InitConfiguration(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 封装要操作的字符
|
|
/// </summary>
|
|
/// <param name="sections">节点配置</param>
|
|
/// <returns></returns>
|
|
public static string app(params string[] sections)
|
|
{
|
|
try
|
|
{
|
|
|
|
if (sections.Any())
|
|
{
|
|
return Configuration[string.Join(":", sections)];
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
public static string GetConnectionString(string key)
|
|
{
|
|
return Configuration.GetConnectionString(key);
|
|
}
|
|
public static IConfigurationSection GetSection(string key)
|
|
{
|
|
return Configuration.GetSection(key);
|
|
}
|
|
}
|
|
}
|