想在任何地方方便调用配置参数。
例如读取PLCIP地址
写一个Appsettingshelper类,如下
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
namespace aaa.Common
{
public class AppSettingsHelper
{
public static IConfiguration Configuration { get; set; }
static AppSettingsHelper()
{
//ReloadOnChange = true 当appsettings.json被修改时重新加载
Configuration = new ConfigurationBuilder()
.Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
.Build();
}
}
}
读写配置文件测试