Windows Forms  

Fixing “The configuration file 'appsettings.json' was not found” in .NET WinForms (.NET 6/7/8)

When working with WinForms + .NET Core applications, many developers face the error:

System.IO.FileNotFoundException: 'The configuration file 'appsettings.json' was not found 
and is not optional. The expected physical path was 
bin\Debug\net8.0-windows\appsettings.json'

This issue usually appears when using:

var builder = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: false);

In simple terms:

The application cannot find appsettings.json at runtime.

This article explains why it happens and how to fix it properly.

Why This Error Happens

In .NET Core WinForms/WPF, the application expects appsettings.json to exist inside the output folder, usually:

bin\Debug\net8.0-windows\

However, Visual Studio does not automatically copy appsettings.json to the output directory.
So, even if the file is present in your project root, the runtime cannot locate it.

Solution 1 — Make appsettings.json Copy to Output Directory

You must instruct Visual Studio to copy this file during build.

Step-by-step

  1. Right-click appsettings.jsonProperties

  2. Change the following:

PropertyValue
Build ActionContent
Copy to Output DirectoryCopy always (or) Copy if newer

Once this is set, the file will always appear in:

bin\Debug\net8.0-windows\

and the error disappears.

Solution 2 — Use AppContext.BaseDirectory (Recommended)

WinForms applications have unpredictable working directories.
So instead of:

Directory.GetCurrentDirectory()

use:

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

Benefits

  • Works in Debug

  • Works in Publish/EXE

  • Works when the EXE is launched from a shortcut

  • Prevents wrong path issues

Combined Final Program.cs (Clean + Correct)

public static IConfiguration AppConfig;

[STAThread]
static void Main()
{
    // 1. Read config from EXE directory
    var builder = new ConfigurationBuilder()
        .SetBasePath(AppContext.BaseDirectory)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

    AppConfig = builder.Build();

    // 2. Setup DI container
    var services = new ServiceCollection();
    services.AddSingleton<IConfiguration>(AppConfig);

    services.AddDbContext<AppDbContext>(options =>
        options.UseSqlServer(AppConfig.GetConnectionString("IPOOffline"))
    );

    services.AddScoped<IWebService, SoapWebService>();
    services.AddTransient<Form1>();

    var provider = services.BuildServiceProvider();

    // 3. Run WinForms App
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(provider.GetRequiredService<Form1>());
}

Summary

If you face:

The configuration file 'appsettings.json' was not found

then follow these 2 rules:

Rule 1

Make sure appsettings.json is set to:

  • Build Action: Content

  • Copy to Output Directory: Copy always

Rule 2

Always use:

AppContext.BaseDirectory

for loading configuration files in WinForms.

Conclusion

This is one of the most common errors developers face when migrating WinForms applications to .NET 6/7/8.

By ensuring that appsettings.json is copied to the output directory and reading configuration from AppContext.BaseDirectory, you can avoid configuration load failures completely.