Ozzie.eu

Love to code although it bugs me.

Creating a console program to run a batch job or some other kind of recurrent task is nice. Even nicer is to be able to pass along some parameters using a configuration file.

If you search the Internet for some help on using a configuration file with a .NET Core console application, you get some pretty good results if you want to use some Dependency Injection/Inversion of Control pattern, or build classes to represent and perform a sort of unmarshalling of the JSON configuration file, or even go through the Microsoft Docs that probably inspired the first two options.

I wanted to just be able to have my simple program, a simple JSON configuration file, and a simple way of reading parameters and do stuff.

Said that, let’s get down to business. Create an empty folder and inside it a simple console application using the dotnet CLI:

dotnet new console


Add a new text file to the folder called “appsettings.json”. To read the JSON config, you’ll need the following two packages from Nuget:

dotnet add package Microsoft.Extensions.Configuration


Include these namespaces on your program, the first to use configuration classes, the second to get access to folder paths:

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


Create a configuration builder from your settings file, passing the full path as an argument:

var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())

var config = builder.Build();