This time I would like to show you the most
important changes in
the
System.Configuration
namespace with .NET 2.0.
I have looked at my blog referrer statistics and saw about 20 hits/day
by google. Most of them were searching
infos how to configure the new Enterprise Library but also a
significant number of people which seem to seek guidance to the
following questions:
- How to read/write
to App.Config?
- How to store a list of objects in a config file via the
System.Configuration mechanism?
Reason enough for me to shed more light on
the System.Configuration namespace.
The main changes from .NET 1.0/1.1 in the System.Configuration
namespace are:
- Write to your App.Config file through the Configuration
class.
- New configuration model for Windows Forms applications.
- Store complex objects including object
collections
in your App.Config File.
- It is possible to store Connection Strings in the
App.Config file see ConnectionSettings this
enables you to store you settings on a SQL Server. The Enterprise
Library for Sample SqlConfiguration exercises this by implementing a
SqlConfigurationSource which can store and retrieve a
ConfigurationSection.
So where to start? I think I show you at first the config file and
explain how you can create it programtically in your application.
The
easiest way to read/write AppSettings
If you want to store only key/value pairs in your App.config file there
is a special section in reserved which allows you to do exactly that.
Simply add an <appsettings> section and add your data as
key/value
pairs of the form <add key="xxx" value="xxxx" />. Thats
all to create new app.config file with settings in it.
App.Config
<?xml
version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Setting1" value="Very" />
<add key="Setting2" value="Easy" />
</appSettings>
</configuration>
Show more ....