Using appsettings.json in a .NET Core console application

- 1 minute read

Most samples one sees about using settings in .NET Core is about setting up an ASP.NET Core application. Very handy but sometimes I simply want to make use of a console application and have that run on some server.

You can add the following code in the Main method of the console application:

var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

IConfigurationRoot configuration = builder.Build();


The appsettings.json file might look like the following:

    "ConnectionStrings": {

In your code file you’ll also need to add the following using statements:

using System.IO;
using Microsoft.Extensions.Configuration;

You’ll also need to install the following Nuget packages for the ConfigurationBuilder, SetBasePath and AddJsonFile to work:

Install-Package Microsoft.Extensions.Configuration
Install-Package Microsoft.Extensions.Configuration.FileExtensions
Install-Package Microsoft.Extensions.Configuration.Json

Note: an important thing to keep in mind is to go to the properties of the appsettings.json file and set the Copy to output directory to Copy if newer or Copy always as otherwise the file itself doesn’t get copied to the folder where the executable is being run and it’ll provide you with exceptions.


Leave a Comment