.NET Core 中提供了非常强大的配置系统以简化配置相关代码的编写方法
.NET Core中的配置系统支持非常丰富的配置源,包括文件(JSON,XML,INI等)、注册表、环境变量、命令行、Azure Key Vault等,配置系统还支持自定义配置源
添加一个Json文件 通过控制台应用读取 使用NuGet下载包然后手动读取配置
Json文件
JSON
{
"Name": "Zakc",
"Proxy":
{
"address": "192.168.1.9",
"Port": 1088
}
}
手动读取配置
JSON
ConfigurationBuilder configbuilder = new ConfigurationBuilder();
configbuilder.AddJsonFile("webconfig.json", optional: false, reloadOnChange: false);
IConfigurationRoot config = configbuilder.Build();
string name = config["Name"];
Console.WriteLine($"配置名称:{name}");
string ProxyAddress = config.GetSection("Proxy:Address").Value;
Console.WriteLine();