At its simplest, the app.config is an XML file with many predefined configuration sections available and support for custom configuration sections. A "configuration section" is a snippet of XML with a schema meant to store some type of information.

  • Overview (MSDN)
  • Connection String Configuration (MSDN)

Settings can be configured using built-in configuration sections such as connectionStrings or appSettings. You can add your own custom configuration sections; this is an advanced topic, but very powerful for building strongly-typed configuration files.

Web applications typically have a web.config, while Windows GUI/service applications have an app.config file.

Application-level config files inherit settings from global configuration files like machine.config. Web also applications inherit settings from applicationHost.config.

Reading from the App.Config

Connection strings have a predefined schema that you can use. Note that this small snippet is actually a valid app.config (or web.config) file:

<?xml version="1.0"?>
<configuration>
    <connectionStrings>   
        <add name="MyKey" 
             connectionString="Data Source=localhost;Initial Catalog=ABC;"
             providerName="System.Data.SqlClient"/>
    </connectionStrings>
</configuration>

Once you have defined your app.config, you can read it in code using the ConfigurationManager class. Don't be intimidated by the verbose MSDN examples; it's actually quite simple.

string connectionString = ConfigurationManager.ConnectionStrings["MyKey"].ConnectionString;

Writing to the App.Config

Frequently changing the *.config files is usually not a good idea, but it sounds like you only want to perform one-time setup.

See: Change connection string & reload app.config at run time which describes how to update the connectionStrings section of the *.config file at runtime.

Note that ideally you would perform such configuration changes from a simple installer.

Location of the App.Config at Runtime

Q: Suppose I manually change some <value> in app.config, save it and then close it. Now when I go to my bin folder and launch the .exe file from here, why doesn't it reflect the applied changes?

A: When you compile an application, its app.config is copied to the bin directory1 with a name that matches your exe. For example, if your exe was named "test.exe", there should be a ("text.exe.config" in .net framework) or ("text.dll.config" in .net core) in your bin directory. You can change the configuration without a recompile, but you will need to edit the config file that was created at compile time, not the original app.config.

1: Note that web.config files are not moved, but instead stay in the same location at compile and deployment time. One exception to this is when a web.config is transformed.

.NET Core

New configuration options were introduced with .NET Core and continue with the unified .NET (version 5+). The way that *.config files works hasn't fundamentally changed, but developers are free to choose new, more flexible configuration paradigms.

As with .NET Framework configuration .NET Core can get quite complex, but implementation can be as simple as a few lines of configuration with a few lines of c# to read it.

  • Configuration in ASP.NET Core
  • Configuration in .NET Core
Answer from Tim M. on Stack Overflow
Top answer
1 of 7
249

At its simplest, the app.config is an XML file with many predefined configuration sections available and support for custom configuration sections. A "configuration section" is a snippet of XML with a schema meant to store some type of information.

  • Overview (MSDN)
  • Connection String Configuration (MSDN)

Settings can be configured using built-in configuration sections such as connectionStrings or appSettings. You can add your own custom configuration sections; this is an advanced topic, but very powerful for building strongly-typed configuration files.

Web applications typically have a web.config, while Windows GUI/service applications have an app.config file.

Application-level config files inherit settings from global configuration files like machine.config. Web also applications inherit settings from applicationHost.config.

Reading from the App.Config

Connection strings have a predefined schema that you can use. Note that this small snippet is actually a valid app.config (or web.config) file:

<?xml version="1.0"?>
<configuration>
    <connectionStrings>   
        <add name="MyKey" 
             connectionString="Data Source=localhost;Initial Catalog=ABC;"
             providerName="System.Data.SqlClient"/>
    </connectionStrings>
</configuration>

Once you have defined your app.config, you can read it in code using the ConfigurationManager class. Don't be intimidated by the verbose MSDN examples; it's actually quite simple.

string connectionString = ConfigurationManager.ConnectionStrings["MyKey"].ConnectionString;

Writing to the App.Config

Frequently changing the *.config files is usually not a good idea, but it sounds like you only want to perform one-time setup.

See: Change connection string & reload app.config at run time which describes how to update the connectionStrings section of the *.config file at runtime.

Note that ideally you would perform such configuration changes from a simple installer.

Location of the App.Config at Runtime

Q: Suppose I manually change some <value> in app.config, save it and then close it. Now when I go to my bin folder and launch the .exe file from here, why doesn't it reflect the applied changes?

A: When you compile an application, its app.config is copied to the bin directory1 with a name that matches your exe. For example, if your exe was named "test.exe", there should be a ("text.exe.config" in .net framework) or ("text.dll.config" in .net core) in your bin directory. You can change the configuration without a recompile, but you will need to edit the config file that was created at compile time, not the original app.config.

1: Note that web.config files are not moved, but instead stay in the same location at compile and deployment time. One exception to this is when a web.config is transformed.

.NET Core

New configuration options were introduced with .NET Core and continue with the unified .NET (version 5+). The way that *.config files works hasn't fundamentally changed, but developers are free to choose new, more flexible configuration paradigms.

As with .NET Framework configuration .NET Core can get quite complex, but implementation can be as simple as a few lines of configuration with a few lines of c# to read it.

  • Configuration in ASP.NET Core
  • Configuration in .NET Core
2 of 7
70

Simply, App.config is an XML based file format that holds the Application Level Configurations.

Example:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="key" value="test" />
  </appSettings>
</configuration>

You can access the configurations by using ConfigurationManager as shown in the piece of code snippet below:

var value = System.Configuration.ConfigurationManager.AppSettings["key"];
// value is now "test"

Note: ConfigurationSettings is obsolete method to retrieve configuration information.

var value = System.Configuration.ConfigurationSettings.AppSettings["key"];
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › framework › configure-apps
Configuring Apps by using Configuration Files - .NET Framework | Microsoft Learn
The configuration system first looks in the machine configuration file for the <appSettings> element and other configuration sections that a developer might define. It then looks in the application configuration file. To keep the machine configuration file manageable, it is best to put these settings in the application configuration file.
🌐
SubMain
blog.submain.com › app-config-basics-best-practices
App.Config: Basics and Best Practices - SubMain Blog
November 3, 2020 - So let’s take a closer look at this file now. When you create a (non-web) .NET Framework application in Visual Studio, an app.config file is added to your project.
🌐
Microsoft Learn
learn.microsoft.com › en-us › visualstudio › ide › how-to-add-app-config-file
Add an app.config file to a project - Visual Studio (Windows) | Microsoft Learn
By adding an application configuration file (app.config file) to a C# project, you can customize how the common language runtime locates and loads assembly files.
🌐
Expo Documentation
docs.expo.dev › workflow › configuration
Configure with app config - Expo Documentation
The app config configures many things such as app name, icon, splash screen, deep linking scheme, API keys to use for some services and so on. For a complete list of available properties, see app.json/app.config.js/app.config.ts reference.
🌐
GitHub
github.com › MicrosoftDocs › visualstudio-docs › blob › main › docs › ide › how-to-add-app-config-file.md
visualstudio-docs/docs/ide/how-to-add-app-config-file.md at main · MicrosoftDocs/visualstudio-docs
By adding an application configuration file (app.config file) to a C# project, you can customize how the common language runtime locates and loads assembly files.
Author   MicrosoftDocs
🌐
App Config
app-config.dev › guide › intro
Introduction | App Config
Configuration comes in the form of a JSON-like structure of values. App Config provides an accessible way to read these values through environment variables, a Node.js API or CLI.
🌐
Vertigisstudio
developers.vertigisstudio.com › getting started
App Config | VertiGIS Studio Developer Center
An app config file is composed of a list of app items. App items can potentially be anything. Each app item has an item $type, which viewer is aware of and knows how to locate and load.
Find elsewhere
🌐
Shopify
shopify.dev › docs › apps › build › cli-for-apps › manage-app-config-files
Manage app config files
You can create, link, and configure Shopify apps directly from your preferred terminal or IDE using Shopify CLI. You can either create a new Shopify app or link to any existing apps. This generates a configuration file in the root directory of your app.
🌐
AWS
docs.aws.amazon.com › aws appconfig › user guide › what is aws appconfig?
What is AWS AppConfig? - AWS AppConfig
AWS AppConfig speeds up software release frequency, improves application resiliency, and helps you address emergent issues more quickly. With feature flags, you can gradually release new capabilities to users and measure the impact of those changes before fully deploying the new capabilities to all users. With operational flags and dynamic configurations, you can update block lists, allow lists, throttling limits, logging verbosity, and perform other operational tuning to quickly respond to issues in production environments.
🌐
Nuxt
nuxt.com › docs › guide › directory-structure › app-config
app.config.ts · Nuxt Directory Structure v4
When your application requires ... you to define multiple layouts and apply them per page. ... Learn more about how to structure your layouts using the layouts/ directory. ... The .nuxtrc file allows you to define nuxt configurations in a flat syntax....
🌐
npm
npmjs.com › package › app-config
app-config - npm
Simple utility for Node.js to load configuration files depending on your environment. Latest version: 1.0.1, last published: 4 years ago. Start using app-config in your project by running `npm i app-config`. There are 1 other projects in the npm registry using app-config.
      » npm install app-config
    
Published   Jul 18, 2021
Version   1.0.1
Author   Pavel Lobodinský
🌐
App Config
app-config.dev
App Config
App Config is a configuration loader with schema validation
🌐
C# Corner
c-sharpcorner.com › article › four-ways-to-read-configuration-setting-in-c-sharp
Four Ways To Read Configuration Setting In C#
November 15, 2021 - This article will demonstrate how we can get/read the configuration setting from Web.Config or App.Config in C#. There are different ways to set the values inside the configuration file and read their values, which are based on the defined keys. We define those values inside the configuration ...
🌐
The Twelve-Factor App
12factor.net › config
The Twelve-Factor App
This is a huge improvement over using constants which are checked into the code repo, but still has weaknesses: it’s easy to mistakenly check in a config file to the repo; there is a tendency for config files to be scattered about in different places and different formats, making it hard to see and manage all the config in one place. Further, these formats tend to be language- or framework-specific. The twelve-factor app stores config in environment variables (often shortened to env vars or env).
🌐
JetBrains
rider-support.jetbrains.com › hc › en-us › community › posts › 360008528140-How-to-create-App-config
How to create App.config? – Rider Support | JetBrains
You've created a .NET Core console application, haven't you? As far as I know, App.config file is for non-Core (full .NET Framework) configuration.
🌐
Farhan Nasim
fnasimbd.github.io › blog › 2016 › 01 › 11 › c-sharp-configurations.html
C# Application Configuration Basics | Farhan Nasim
September 30, 2019 - Both methods use application’s configuration file (App.config) as data source. Both reads the configurations from file on application startup, converts them to CLR types (e.g. string, int, etc), and serves them with a static property: ConfigurationManager.Appsettingsin the first case and ...