C#
Windows Forms
configuration file
app settings
application development

Simplest way to have a configuration file in a Windows Forms C application

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

For a classic Windows Forms application, the simplest built-in configuration file is App.config. It lets you keep environment-specific values out of code and read them through ConfigurationManager. That is the right default for fixed application settings such as URLs, feature flags, and timeouts, while user-editable preferences are better handled as user-scoped settings.

Basic App.config Setup

In a WinForms project, add an App.config file and define values under appSettings.

xml
1<?xml version="1.0" encoding="utf-8" ?>
2<configuration>
3  <appSettings>
4    <add key="ApiBaseUrl" value="https://api.example.com" />
5    <add key="RefreshSeconds" value="30" />
6  </appSettings>
7</configuration>

At build time, this becomes YourApp.exe.config next to the executable.

Reading Values in C#

To read settings, use ConfigurationManager.AppSettings.

csharp
1using System;
2using System.Configuration;
3
4class Program
5{
6    static void Main()
7    {
8        string apiBaseUrl = ConfigurationManager.AppSettings["ApiBaseUrl"];
9        string refreshSeconds = ConfigurationManager.AppSettings["RefreshSeconds"];
10
11        Console.WriteLine(apiBaseUrl);
12        Console.WriteLine(refreshSeconds);
13    }
14}

For simple application configuration, this is usually enough.

Parsing Typed Values Safely

AppSettings returns strings, so convert them explicitly and validate bad input.

csharp
1using System;
2using System.Configuration;
3
4int refreshSeconds;
5
6if (!int.TryParse(ConfigurationManager.AppSettings["RefreshSeconds"], out refreshSeconds))
7{
8    refreshSeconds = 30;
9}
10
11Console.WriteLine(refreshSeconds);

Do not assume configuration values are always valid. Treat them like external input.

Add the Required Package if Needed

In older .NET Framework WinForms projects, ConfigurationManager is commonly available by default. In newer project styles, you may need the package explicitly.

bash
dotnet add package System.Configuration.ConfigurationManager

If the code compiles without that step in your project, no extra action is needed.

When to Use User-Scoped Settings Instead

App.config is good for values deployed with the app. It is not ideal for preferences that users should change at runtime, such as window size, theme, or last-opened file path.

For that, use application settings generated by Visual Studio and accessed through Properties.Settings.Default.

csharp
Properties.Settings.Default.WindowTitle = "Admin Console";
Properties.Settings.Default.Save();

This writes user-specific values to a per-user config store instead of trying to edit the application config file beside the executable.

Read-Only Deployed Config Versus Writable User Config

This distinction matters in production. Files deployed under Program Files are often not writable for normal users. So if your application needs end-user changes during runtime, storing those changes in App.config is the wrong model.

A practical rule:

  • use App.config for deployment-time settings
  • use user-scoped settings for runtime preferences

That split keeps configuration predictable and avoids permission problems.

Example in a Form

Here is a simple WinForms form loading a configured title:

csharp
1using System.Configuration;
2using System.Windows.Forms;
3
4public partial class MainForm : Form
5{
6    public MainForm()
7    {
8        InitializeComponent();
9
10        string title = ConfigurationManager.AppSettings["WindowTitle"] ?? "My App";
11        this.Text = title;
12    }
13}

And the matching config entry:

xml
<appSettings>
  <add key="WindowTitle" value="Operations Console" />
</appSettings>

This is a clean example of keeping environment or branding values out of the compiled code.

Alternatives

You can use JSON files such as appsettings.json in desktop apps too, especially in newer .NET applications that already use Microsoft.Extensions.Configuration. But if the question is the simplest built-in option for a typical WinForms app, App.config still wins on setup cost and familiarity.

Use JSON only if:

  • the application already uses modern configuration abstractions
  • you need hierarchical settings
  • you want consistency with backend .NET services

Otherwise, App.config is easier.

Common Pitfalls

The biggest mistake is trying to store user-editable runtime preferences in App.config and then discovering the deployed file is not writable. Another is forgetting that AppSettings values are strings and need parsing. Developers also sometimes mix several configuration styles without a clear reason, which makes the app harder to maintain. Finally, missing ConfigurationManager references can look confusing until you know the package or assembly requirement depends on project type.

Summary

  • For a WinForms app, the simplest configuration file is App.config.
  • Read fixed settings through ConfigurationManager.AppSettings.
  • Parse configuration values explicitly instead of assuming valid types.
  • Use user-scoped settings for preferences that change at runtime.
  • Prefer App.config over more complex configuration stacks unless the app genuinely needs them.

Course illustration
Course illustration

All Rights Reserved.