How to add app.config file and read content in .NET Core project

Updated on: February 26, 2023

In .NET Core, we have appsettings.json files to add our AppSettings and DB Connection strings, but sometimes, we require a need to use app.config file which is getting used in .NET Framework project for keeping configurations. 

For this Article, we are using Visual Studio 2022 and .NET 7 and we have created a Console application to see the usage of app.config file, we can create Console App from Visual Studio or also with below .NET CLI Commands as shown below:

F:\SampleApps\DotNetCore\Console>dotnet new console -o AppConfigConsoleApp
F:\SampleApps\DotNetCore\Console>cd AppConfigConsoleApp
F:\SampleApps\DotNetCore\Console\AppConfigConsoleApp>dotnet new sln
F:\SampleApps\DotNetCore\Console\AppConfigConsoleApp>dotnet sln add AppConfigConsoleApp.csproj

So, here it has created a Console App project with name "AppConfigConsoleApp.csproj" and it also created solution file and also project added to Solution and we have opened this solution in Visual Studio 2022, so now it looks like below:

Now, we will add app.config file in the project, right click on the Solution name in the Solution Explorer and click on "Add => New Item" and select Class/Interface or any Item and give it name as "app.config" and click on "Add" button.

it will create app.config file, replace the content of this file with below content:

<?xml version="1.0" encoding="utf-8"?>
    <add key="ProductName" value="AppConfig Product"/>

Also, make sure we are setting "Copy to Output Directory" property of this file to "Copy always" or "Copy if newer" so that after every build/publish, it will copy the file into debug/release in bin folder:

Now, we will open "Program.cs" and add the below content:

 internal class Program
        static void Main(string[] args)
            var productName = System.Configuration.ConfigurationManager.AppSettings["ProductName"];//Here we are reading ProductName from app.config file
            Console.WriteLine($"Product Name is: {productName}");

After, running above program, we are getting following output: