How to add a configuration file (app.config) to your #C application?

How to add a configuration file (app.config) to your #C application?
3.7 (73.33%) 3 votes

Hi,

This article shows how to add a simple configuration file (app.config) to your #C project. Sooner or later, you need to change a value such database connection string or username. 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 which means you can create keys to be used on your project without need to recompile it everytime you need to update some value.

 

What is App.config?

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.

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, e.g. the machine.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:

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.

The easy way to get data is using dot walking through ConfigurationManager.

In meantime I personally prefer to use a specific class to retrieve my values.  For example, if you add a new class file to your project named “InitialDefinition.cs” and write the following:

Once you have done it, you can read data on your main class using the following code:

And voilá. The string connectionType already contains the value “SQL” as specific on “app.config” file.

Writing on 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 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.

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.

 

I hope you find this helpful.

Thank you,

RMC

Back To Top