.NET CORE 获取配置文件信息
程序员文章站
2024-01-04 15:50:34
...
.NET CORE 获取配置文件信息
动态获取appsettings.json文件里的配置信息
public class AppSetting
{
private static readonly object objLock = new object();
private static AppSetting instance = null;
private IConfigurationRoot Config { get; }
private AppSetting()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
Config = builder.Build();
}
public static AppSetting GetInstance()
{
if (instance == null)
{
lock (objLock)
{
if (instance == null)
{
instance = new AppSetting();
}
}
}
return instance;
}
public static string GetConfig(string name)
{
return GetInstance().Config.GetSection(name).Value;
}
}
注意:在类库里写这个代码时会提示ConfigurationBuilder缺少引用,或者找不到SetBasePath方法,这时你需要引用以下NuGet包:
Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.FileExtensions
Microsoft.Extensions.Configuration.Json
推荐阅读
-
.NET Core在类库中读取配置文件appsettings.json
-
.NET CORE 获取配置文件信息
-
jenkins 读取json文件_干货:.net core实现读取appsettings.json配置文件(建议收藏)...
-
.Net Core 配置文件的使用
-
.net core读取配置文件appsettings.json
-
.NET Core读取配置文件appsettings.json
-
.NET Core 读取appsettings.json配置文件
-
Asp .Net Core 读取appsettings.json配置文件
-
Asp .Net Core 读取appsettings.json配置文件
-
asp.net core 读取Appsettings.json 配置文件