Console.Config\Program.cs
using Microsoft.Extensions.Configuration;
var basePath = Directory.GetCurrentDirectory();
var configuration = new ConfigurationBuilder()
.SetBasePath(basePath) // 设置基本路径
.AddJsonFile("appsettings.json") // 添加配置文件
.Build();
var dataProvider = configuration["DataProvider"] ?? throw new InvalidOperationException("DataProvider在appsettings.json未发现");
var connection = configuration.GetConnectionString(dataProvider)?? throw new InvalidOperationException($"{dataProvider}在appsettings.json未发现");
System.Console.WriteLine(connection);
Console.Config\appsettings.json
{
"ConnectionStrings": {
"MsSqlConnectionDev": "Data Source=localhost;Initial Catalog=test6;User ID=sa;Password=song2030..;Trust Server Certificate=True",
"MsSqlConnection": "Server=localhost;Database=Electric;Uid=root;Pwd=123456;"
},
"DataProvider": "MsSqlConnection"
}
标签:Console,配置文件,appsettings,var,json,dotnet,dataProvider
From: https://www.cnblogs.com/zhuoss/p/18164030