How to use app.config or configuration files in .NET Core Console Application

Hello everyone and welcome to my blog. In today’s blog I will show you on how to use configuration files in .NET Core console application.

For .NET framework applications we always use the app.config to store our configuration values for the application. In .NET Core, instead of App.Config file, we need to use appsettings.json file. So let’s get started on how to use appsettings.json and read values from there in your code.

For this project I will use a console application using .NET 6.0

Install the following Nuget packages.

Once they are installed successfully, add using statements in your code to refer to them.

Next step is to add .json file with the name appsettings.json to your project. While the name does not need to be always appsettings, this is a naming convention we follow.

Add he required configuration values to your appsettings.json file. Below is my sample content for the appsettings.json file.

Now a very important step and something which is missed out frequently. Right click on your appsettings.json file and then click on Properties.

Set the Build Action to Content and Copy to output directory to Always. Save the changes.

Add the following lines of code to retrieve the configuration values from appsettings.json file.

// load the configuration file.
var configBuilder = new ConfigurationBuilder().
   AddJsonFile("appsettings.json").Build();

// get the section to read
var configSection = configBuilder.GetSection("AppSettings");

// get the configuration values in the section.
var client_id = configSection["client_id"] ?? null;

Well that’s it. When you run the code, you should now be able to read the values from your configuration file.

Hope you liked the post. If this post has helped you, you can buy me a coffee.

For similar interesting posts on Microsoft.NET and Power Platform, subscribe to my blog and stay updated. Link to subscribe on the right hand pane.

You can also check out the below posts.

Debajit Dutta
Business Solutions MVP