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
🌐
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.
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"];
🌐
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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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.
🌐
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.
🌐
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
🌐
Delft Stack
delftstack.com › home › howto › csharp › app.config in csharp
App.Config in C# | Delft Stack
October 12, 2023 - App.Config, or the Application Level Configurations file, is an XML-based file containing the predefined configuration sections available and allows for custom configuration sections that you can modify.
🌐
App Config
app-config.dev
App Config
App Config is a configuration loader with schema validation
🌐
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....
🌐
Microsoft Learn
learn.microsoft.com › en-us › windows › win32 › sbscs › application-configuration-files
Application Configuration Files - Win32 apps | Microsoft Learn
An application configuration file is an XML file used to control assembly binding. It can redirect an application from using one version of a side-by-side assembly to another version of the same assembly.
Top answer
1 of 5
62

Right click on application->Go to Add->you will see the exact picture What i have attached here->Pick the Application Config File.

2 of 5
9

Well, the other answers might have worked for others, but for me, adding a settings file to the project's properties solved the problem - it actually serialized the settings (which are editable via a visual designer) to the config file for me. So this way, the config file approach showed in the other answers here didn't work for me, but instead creating a settings file did work.

The steps are:

Project (not solution) > Add > New Item > Settings File

In addition, you might want to have your settings available in your code with strongly-typed values. I did the following:

  1. renamed the settings file to something useful - mine was "Settings.settings"
  2. moved this file to the "Project > Properties" section
  3. double-clicked the settings file icon
  4. In the designer, added settings keys and values
  5. Viola! You have the settings available in your app, with strongly-typed values!

So, now I could access my settings like this (from my console app):

bool boolSetting = Properties.Settings.Default.is_debug_mode;

After compilation, I found that these settings are stored automatically in a file named "AssemblyName.exe.config", alongside the console binary itself in the Debug directory.

So, I think this is a cleaner, and more flexible way of creating and managing the app's config file.

NOTE: Am running Visual Studio Ultimate 2012, and am building a .NET 3.5 console app.

🌐
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ý