ASP.NET Core support a new configuration system as seen here: https://docs.asp.net/en/latest/fundamentals/configuration.html
Is this model also supported in .NET Core console applications?
If not what is alternate to the previous app.config
and ConfigurationManager
model?
For a .NET Core 2.0 console app, I did the following:
{ "myKey1" : "my test value 1", "myKey2" : "my test value 2", "foo" : "bar" }
Configure to copy the file to the output directory whenever the project is built (in VS -> Solution Explorer -> right-click file -> select 'Properties' -> Advanced -> Copy to Output Directory -> select 'Copy Always')
Install the following nuget package in my project:
Add the following to Program.cs (or wherever Main()
is located):
static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
var configuration = builder.Build();
// rest of code...
}
Then read the values using either of the following ways:
string myKey1 = configuration["myKey1"];
Console.WriteLine(myKey1);
string foo = configuration.GetSection("foo").Value;
Console.WriteLine(foo);