When you start a new .NET 6 Console app, you will have little more than what you see here.
If one wants to use the json configuration files typically used in a web app, there are a few things we need to add.
(1) Add package references to the following:
Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.Configuration.FileExtensions
(2) Right-click on the project, add new item, Json configuration file. Call it "appsettings.json". Then add some settings in the file, eg.
{
"TestSettings": {
"Option1": "some string value",
"Option2": 42
}
}
Now just setup your host and builder, read the config file values and use them. In program.cs use something like this
using Microsoft.Extensions.Configuration;
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false);
IConfiguration config = builder.Build();
var Option1 = config.GetSection("TestSettings:Option1");
string Opt = Option1.Value;
Console.WriteLine($"The answer is {Opt}");