Tuesday, July 09, 2013

Why, Where, and How of .NET Configuration Files

Introduction
I've been working with .NET configuration files for a number of years now. I thought it might be helpful to others if I provided a quick introduction to them.

Within this document, many of the C# code examples assume you have both included a reference to System.Configuration.dll in your project and that you have included the following using statement in your code:

using System.Configuration;

Both of these are necessary to access the ConfigurationManager class, which is one method of accessing configuration information.

Why
The .NET framework provides a rich set of classes, and techniques, to simplify application configuration. Essentially, all of these classes make it easy to read and write that configuration information from an XML configuration file.

The configuration file includes a number of standard sections, some custom sections for common .NET features, and also allows the developer to create their own custom configuration sections.

The standard sections have evolved over time. Initially, standard configuration was done mostly through the appSettings section, which contains name / value pairs for each setting. Over time, transparent, type-safe support was provided via a generated C# Settings class and the corresponding applicationSettings and userSettings configuration sections.

Where
Where do I find the configuration file? This is a deceptively complicated problem. Since configuration is hierarchical, there are actually multiple configuration files that may affect an application. These include the machine configuration file, the application (or web) configuration file, the user local settings file, and the user roaming settings file.

Machine Configuration

The machine configuration file lives with the, not so easily found, .NET framework files. The location of the configuration file is dependent on the version of .NET and type of platform (e.g. 64 bit) used by your application.

A typical example, might be: C:\Windows\Microsoft.NET\Framework\v4.0.30319\CONFIG\machine.config

In your C# application code, the following will return the location of the file:

System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() + @"CONFIG\machine.config"

Application Configuration

The application configuration file usually lives in the same directory as your application. For web applications, it is named Web.config. For non-web applications, it starts life with the name of App.config. Following a build, it is copied to the same name as your .exe file. So, for the program MyProgram.exe, you might expect to find MyProgram.exe.config, in the same directory.

In your C# application code, the following will return the location of the file:

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

While it is not generally recommended, you may find that some applications alter the location of the application configuration file as follows:

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "NewName.config")

Read more: Codeproject
QR: Inline image 1