Using XML Based Configuration File in Windows Form Applications

Often when building an application, a programmer wants to be able to change application settings without having to recompile the application. There are several ways to accomplish this. .Net provides an easy way to do this using the application configuration file.

Configuration File

A configuration file contains configuration settings that the application can read.
Here is a sample of what a file of this type looks like:

<?xml version="1.0" encoding="utf-8" ?>
<
configuration>
<
appSettings>
<
add key="LabelColor" value="Yellow" />
<
add key="MaximumWeight" value="150" />
<
add key="Title" value="Watcher" />
</
appSettings>
</
configuration>

The configuration file should be in the same directory as the application. The name of the configuration file should have the same name as the application with .config at the end. For example, an application called Watcher.exe should have a configuration file called Watcher.exe.config.

Accessing Configuration File

To access the appSettings values from inside the program, use the AppSetting property of the ConfigurationSettings class.

string title = System.Configuration.ConfigurationSettings.AppSettings["Title"];

Two things should be kept in mind when accessing appSettings from the configuration file.One, the AppSetting could be null. Two, the AppSetting property always returns a string.When getting values from the configuration file, the code needs to handle these situations.

One way to approach this is as follows:

int AppSetValueMax = 0;
if (ConfigurationSettings.AppSettings[key] != null)
{
try
{
AppSetValueMax = Convert.ToInt32(ConfigurationSettings.AppSettings["Max"]);
}
catch(Exception e)
{
//Exception Handling
}
}

Class for Accessing Configuration File Settings

Instead of writing all those lines of code every time a setting from the configuration file is needed, it would be nice to just write one line of code. A way to do this would be to use a class that managed all of the details. The class would need just one overloaded method. The method takes two parameters, the name of the AppSetting and a default value to use if for some reason the value in the configuration file cannot be accessed. All the casting and exception handling are dealt with in the method. In the code below the method
GetAppSetting does this:

button1.Text = AppSet.GetAppSetting("button1.Text", "Submit");
label1.BackColor = AppSet.GetAppSetting("label1.BackColor", Color.WhiteSmoke);
double Result = 450 / AppSet.GetAppSetting("temp", 2.5);

Here are some examples of the overloaded method GetAppSetting:

public static string GetAppSetting(string key, string DefaultValue)
{
return (ConfigurationSettings.AppSettings[key] != null)
? ConfigurationSettings.AppSettings[key]
: DefaultValue;
}
public static double GetAppSetting(string key, double DefaultValue)
{
double AppSetValue = DefaultValue;
if (ConfigurationSettings.AppSettings[key] != null)
{
try
{
AppSetValue = Convert.ToDouble(ConfigurationSettings.AppSettings[key]);
}
catch(Exception e)
{

MessageBox.Show("Error converting the value in the config file.\n"+e.Message+"\nUsing the Default Value: " + DefaultValue + " for " + key);
}
}
return AppSetValue;
}
public static Color GetAppSetting(string key, Color DefaultValue)
{
Color AppSetValue = (ConfigurationSettings.AppSettings[key] !=
null)
//If name is not the valid name of a pre-defined color, the //FromName method
//creates a Color structure that has an ARGB value of zero.
? Color.FromName(ConfigurationSettings.AppSettings[key])
: DefaultValue;
return AppSetValue;
}

The class that contains the GetAppSetting method can be added to a utility library that can be used with any application.


Similar Articles