SIGN UP MEMBER LOGIN:    
ARTICLE

Using XML Based Configuration File in Windows Form Applications

Posted by Daniel Olson Articles | XML in C# April 15, 2002
Often when building an application, a programmer wants to be able to change application settings without having to recompile the application.
Reader Level:
Download Files:
 

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.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor