1. 使用Microsoft.Extensions.Configuration、Microsoft.Extensions.Configuration.Json插件,读取Json文件数据
(1)创建appsettings.json文件(注:设置属性中始终复制)
{
"appsettings":
{
"name": "ceshi",
"version": "1.0.0"
}
}
(2)主要代码文件
#region 使用Microsoft.Extensions.Configuration、Microsoft.Extensions.Configuration.Json插件,读取Json文件数据
{
//构造配置对象
ConfigurationBuilder builder= new ConfigurationBuilder();
builder.AddJsonFile("appsettings.json");
var configuration=builder.Build();
//读取配置数据
var name = configuration["appsettings:name"];
var version= configuration["appsettings:version"];
Console.WriteLine($"name:{name}");
Console.WriteLine($"version:{version}");
}
#endregion
2. 使用Microsoft.Extensions.Configuration、Microsoft.Extensions.Configuration.Xml插件,读取Xml文件数据
(1)创建appsettings.xml文件(注:设置属性中始终复制)
<configuration>
<Appsettings>
<name>ceshi</name>
<version>1.0.0</version>
</Appsettings>
</configuration>
(2)主要代码文件
#region 使用Microsoft.Extensions.Configuration、Microsoft.Extensions.Configuration.Json插件,读取Json文件数据
{
//构造配置对象
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.AddXmlFile ("appsettings.xml");
var configuration = builder.Build();
//读取配置数据
var name = configuration["appsettings:name"];
var version = configuration["appsettings:version"];
Console.WriteLine($"name:{name}");
Console.WriteLine($"version:{version}");
}
#endregion