# Setup Multiple Settings Files with a .NET Console Application

**Date:** 2019-01-03  
**Author:** Kees C. Bakker  
**Categories:** .NET / C#  
**Tags:** Console Application  
**Original:** https://keestalkstech.com/setup-multiple-setting-files-with-a-net-console-application/

![Setup Multiple Settings Files with a .NET Console Application](https://keestalkstech.com/wp-content/uploads/2018/12/Use-multiple-appsettings.json-in-a-Console-Application.jpg)

---

In *ASP.NET Core* we are used to having multiple `appsettings.json` files with settings that differ per environment. I want to do the same in a *Console Application*. This makes debugging the application easier.

## Multiple environments

First, I would like to distinguish between environments. For ASP.NET Core, I use *Local*, *Development* and *Production*. Because I don't have a *Development* environment for my application, I'll only use *Local*.

Just like ASP.NET Core, we will specify the environment as an *environment variable*. Open `Properties/launchSettings.json` and add the variable to your active profile:

```json
{
  "profiles": {
    "ConsoleApp": {
      "commandName": "Project",
      "environmentVariables": {
        "ENVIRONMENT": "Local"
      }
    }
  }
}
```

In ASP.NET this variable is named `ASPNETCORE_ENVIRONMENT`.

## Multiple appsettings

Add the following JSON files to the root of your application:

In my case, I don't use a special *Production* file, but I'll override local setting values in the appsettings.Local.json file. Next, we must create a settings class, something like:

```cs
public class AppSettings
{
    public string TempDirectory { get; set; }
}
```

Open the appsettings.json and add an `app` section:

```json
{
    "App": {
        "TempDirectory": "c:\\temp\\rss-hero\\tmp\\"
    }
}
```

Note: I only add settings that differ from production in my local settings file. The system will override the settings of the main file.

## Hook 'em up!

Next, we need to hook the settings up in the application. In a [previous article I've demonstrated how to leverage dependency injection](https://keestalkstech.com/2018/04/dependency-injection-with-ioptions-in-console-apps-in-net-core-2/) to support `IOptions` and we're building upon that. Install the following packages:

Now add this code to your `Main` class:

```cs
var environmentName =
    Environment.GetEnvironmentVariable("ENVIRONMENT");

// create service collection
var services = new ServiceCollection();

// build config
var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", false)
    .AddJsonFile($"appsettings.{environmentName}.json", true)
    .AddEnvironmentVariables()
    .Build();

// setup config
services.AddOptions();
services.Configure<AppSettings>(configuration.GetSection("App"));

// create service provider
var serviceProvider = services.BuildServiceProvider();
```

We can now resolve settings using dependency injection or directly by doing:

```cs
var appSettings = serviceProvider
    .GetService<IOptions<AppSettings>>();
```

## AddEnvironmentVariables?

The environment variables are used as a way to *override* the app settings. In this case we can override the setting adding an environment variable with the name `App__TempDirectory`. The `__` can be used to set deeper level settings.

## That's all

Setting this up shouldn't be more than copy-paste. For a more comprehensive overview of dependency injection for console applications check: [Dependency injection (with IOptions) in Console Apps in .NET Core](https://keestalkstech.com/2018/04/dependency-injection-with-ioptions-in-console-apps-in-net-core-2/).

## Changelog

- 2026-07-19: Replaced debug-tab instructions with `launchSettings.json` approach.
- 2019-09-19: Explained how `AddEnvironmentVariables` can be used to override settings.
- 2019-01-03: Initial article.
