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 DS.Module.Core { /// /// 配置读取类 /// public class AppSetting { public static IConfiguration Configuration { get; set; } static AppSetting() { string Path = "appsettings.json"; //ReloadOnChange = true 当appsettings.json被修改时重新加载 Configuration = new ConfigurationBuilder() // .SetBasePath(contentPath) .Add(new JsonConfigurationSource { Path = Path, Optional = false, ReloadOnChange = true }) .Build(); } /// /// 构造函数 /// /// public AppSetting(IConfiguration configuration) { Configuration = configuration; } /// /// 封装要操作的字符 /// /// 节点配置 /// public static string app(params string[] sections) { try { if (sections.Any()) { return Configuration[string.Join(":", sections)]; } } catch (Exception) { } return ""; } /// /// 递归获取配置信息数组 /// /// /// /// public static List app(params string[] sections) { List list = new List(); // 引用 Microsoft.Extensions.Configuration.Binder 包 Configuration.Bind(string.Join(":", sections), list); return list; } /// /// 根据路径 configuration["App:Name"]; /// /// /// public static string GetValue(string sectionsPath) { try { return Configuration[sectionsPath]; } catch (Exception) { } return ""; } } }