I was working with developers recently who are new to the
.NET environment, and they were scratching their heads at the concept of
storing configuration information. They were stuck on the older concept of
using initialization (ini) or text files. I steered
them away from this avenue toward a more appropriate .NET avenue via XML. While
both ASP.NET Web Forms and Windows Forms provide configuration files for
storing application data, you can always fall back on one of the main features
of .NET: XML. Let’s take a closer look at storing application data in an XML
file.

XML is the key

XML is the backbone of the many technologies involved in the
Web services movement, as well as being a standard feature of the .NET
Framework. With that in mind, we can easily take advantage of XML and related
features to store application-specific data. The process begins with defining
the structure or details of the data we will be using.

A structure or class can be created to work with the data.
In our simple example, we will be storing the application name, window title,
and some text entered by the user. The values are maintained via class
properties. You could use the C# class that’s in
Listing A.

One key aspect of this code is that the class is serialiazable, so this makes it possible to serialize an
instance of the class to disk, thus maintaining its state. Next, we utilize
this class in a very basic Windows Forms application. The class properties are
used to populate two label controls. In addition, the user may enter text via a
text field. The data entered in the text field is saved by way of the CustomText property.
Listing B contains the sample C# code. Listing C contains the equivalent VB.NET code follows with the class presented
first. Listing D features the
application code.

Weekly .NET tips in your inbox

TechRepublic’s free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET.

Automatically sign up today!

Upon loading the form, the XML file is used to populate the
controls on the form. If the file does not exist, it is created and populated.
The form contains one button that saves the data to the XML file when clicked.
Since it is a configuration file, you may need to automatically save the file
when/if the form is closed or data values change.

There are a few differences between the C# and VB.NET
versions, but they are basically the same. One key difference between the two
languages is case-sensitivity. VB.NET is not case-sensitive, so an underscore
was added to the class member variables to differentiate from the property
names whereas it isn’t a problem in C#. A key aspect of this simple application
is serialization.

Serialization

Serialization allows developers to persist objects by
storing them in files. This includes the objects data and state. The files may
be stored on a disk drive, database, and so forth. The .NET Framework provides
various namespaces for serializing objects. In this example, we utilize the System.Runtime.Serialization namespace. To serialize an
object, you need to either mark the object class with the [Serializable]
attribute or implement the ISerializable interface.
As stated earlier, we created our Config class as Serializable by including the Serializable
attribute. Serialization was covered in greater detail in a previous column.

Location

Using XML to store configuration data is great, but one
important issue is deciding where to store the serialized file. One option is a
backend database like SQL Server. In our sample application, the root of the
local C drive was used, but this may not be appealing. Microsoft recommends
storing application data in one of three locations accessible with the System.Environment class: These three directories are
accessed via calls to the GetFolderPath method using
the three values:

  • Environment.SpecialFolder.ApplicationData: This is the
    directory for the current user, shared by all machines on the network.
  • Environment.SpecialFolder.CommonApplicationData: This
    is the directory for storing information that is shared by all users on
    all machines.
  • Environment.SpecialFolder.LocalApplicationData: This
    is the directory for the current user that is only available when logged
    on to this machine.

The code snippet in
Listing E displays the settings for each of these variables on my machine. This listing also contains the output for the code snippet. Note: This is only
a recommendation; you may choose to use a different directory.

Maintaining application data

The .NET Framework provides many ways to maintain
application-specific configuration data. ASP.NET and Windows Forms applications
have unique configuration files with associated programming models, but you can
easily take advantage of the power of XML to store and maintain configuration
information.