How to use a App.config file in WPF applications?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In a WPF application, App.config is the traditional place to store simple settings, connection strings, and custom configuration sections. At build time it becomes YourApp.exe.config, and the usual runtime entry point for reading it is ConfigurationManager.
Start With A Simple appSettings Section
For basic key-value settings, appSettings is enough:
This file lives in the project as App.config, but the running program reads the generated .exe.config copy in the output directory.
Read Values With ConfigurationManager
For .NET Framework WPF apps, ConfigurationManager is the usual API. In newer .NET versions, you may need the System.Configuration.ConfigurationManager package.
This pattern is fine for small applications, but once several settings are used repeatedly, a wrapper class usually reads better.
Create A Strongly Typed Wrapper
Instead of scattering raw string keys across the codebase, centralize configuration access:
This avoids magic strings in your view models and services.
Use It Naturally In WPF And MVVM
Configuration values are often consumed in the startup path or view models:
That makes configuration part of the application's bootstrapping logic instead of an ad hoc concern spread throughout the UI.
Connection Strings And Custom Sections
For database access, use the connectionStrings section rather than stuffing connection data into ordinary appSettings.
If your configuration grows more complex, you can define a custom configuration section type. That is still a valid App.config pattern in classic WPF applications, although many teams stop at appSettings plus connection strings unless the structure becomes genuinely complicated.
Know The Runtime File You Are Actually Editing
One source of confusion is that editing App.config while the application is already running does not magically change the in-memory values. The running process reads YourApp.exe.config, and values are cached. If you modify the config at runtime, you need to update the executable configuration file and refresh the relevant section.
That is why App.config is best treated as deployment-time configuration rather than a general user settings database. For per-user mutable settings, the built-in settings designer or application data storage is often a better fit.
Common Pitfalls
- Editing
App.configin the project and expecting the running executable to reread it automatically. - Scattering raw configuration key strings across the codebase instead of wrapping them once.
- Putting connection strings into
appSettingsinstead of the dedicatedconnectionStringssection. - Storing secrets in plain text configuration files that ship beside the executable.
- Treating
App.configas a user preferences store when the values are really application-level settings.
Summary
- '
App.configis the classic XML configuration file for WPF applications.' - Read simple values with
ConfigurationManager.AppSettingsand connection strings withConfigurationManager.ConnectionStrings. - Wrap configuration access in a typed helper so WPF and MVVM code stays clean.
- Remember that the runtime file is the generated
.exe.config, not the source project file itself. - Write settings at runtime with
OpenExeConfiguration+Save+RefreshSection. - For .NET 6+ WPF apps, consider
appsettings.jsonwithIConfigurationas the modern alternative.
Related reading
- How to use async with Visual Studio 2010 and .NET 4.0?
- How to use async/await on void methods in ASP.NET Core?
- How to use AutoMapper .ForMember?
- How to use Bootstrap 4 in ASP.NET Core
- How to use C8 IAsyncEnumerableT to async-enumerate tasks run in parallel
- How to use dependency injection with an attribute?
- How to use HttpWebRequest .NET asynchronously?
- How to use HttpWebRequest .NET asynchronously?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.