Creating and using an Application Configuration File

The Application Configuration File is actually an XML file where you can store configuration information of an application. The information is stored as a key/value pair.

For example, you are creating an application that needs to list up all files in a directory. It’s possible to hardcode the path to the direcotry in your code. But if you do this, it would be difficult to change this path. Every time you need to change this path, you’ll need to rebuild your application. It would be wiser to store the path in the configuration file of the application.

It’s possible to open and edit a configuration file with any text editor including notepad. An application will load the values from within the configuration file. You do not have to recompile and rebuild the application when changing a stored value in the configuration file. So it’s always possible to change the values after deployment of the application.

Visual Studio

The configuration file for an applications in Visual Studio will be named App.config by default. This file will not be automatically created when creating a new application. It’s possible to add it to your application when needed.

To create a configuration file, right click on the project name in the Solution Explorer. Choose Add > New item and select ‘Application Configuration File‘ from the list of choices. This will create the App.config file.

The App.config file will look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>

You can store custom configuration information in a XML element as a key/value pair.

<add key="key" value="value" />

You have to place the key/value pairs into the appSettings element. The appSettings element stores custom application configuration information such as database connection strings, file paths, XML Web service URLs, or any information stored in an application’s .ini file. The key/value pairs specified in the appSettings element can be accessed in code.

Example configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
  <add key="DirectoryPath" value="c:\TEMP\" />
 </appSettings>
</configuration>

To access the configuration settings from App.config, use the following code in the application:

string directory = ConfigurationManager.AppSettings["Directory"];

The ConfigurationManager class provides access to configuration files for client applications. This class is part of the namespace System.Configuration. We have to add a reference to the System.Configuration.dll so we can use the ConfigurationManager class and add a using directive on top of our code.

Example:

using System;
using System.Configuration;
using System.IO;

namespace Application
{
    class Program
    {
        static void Main(string[] args)
        {
            string directory = ConfigurationManager.AppSettings["Directory"];
            string[] files = Directory.GetFiles(directory);
        }
    }
}

In this example. First, the application will retrieve the directory path (c:\TEMP\) from the configuration file. Then it will get the names of files (including their paths) from the directory and put them in an array.

After building the application, Visual Studio will automatically create a file called [application name].exe.config in the bin\debug folder. The content of the App.config file will be copied over to this configuration file. Now it’s possible to change values in this configuration file and run the application with the new values without the need of rebuilding the application again. The [application name].exe will read the data from [application name].exe.config file.

Advertisements

Comments are closed.

%d bloggers like this: