Browse DevX
Sign up for e-mail newsletters from DevX


How to Make Your .NET Windows Forms Configuration Files Dynamic  : Page 5

Although Windows Forms configuration files are read-only within the framework, they become dynamic XML databases when you create this AppSettingsManager class.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Using the AppSettingsManager
To use the AppSettingsManger, create an instance, and then call its methods and properties freely. If you want to save the settings you've created, you don't need to do anything—the class automatically saves settings alterations when it's finalized. If you want to save the settings earlier than that, call the Save() method to force an immediate save. If you want to abandon changes that you've made, call the AbandonChanges() method.

The code is somewhat complicated by the fact that configuration settings can be either single-valued or multi-valued. All settings exist in tags that look like this:

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

Single-valued settings contain a single value for the "value" attribute, whereas multi-valued settings contain a comma-delimited list of values, so that you can store multiple values associated with a single key. For example, you might wish your application to open its main window at the same size it was when the application last closed. Rather than have two keys, one for the Width and one for the Height, it's probably more convenient to have a single Size key, such as:

<add key="size" value="457,324"/>

To retrieve a single-valued setting, use the GetValue() method, passing the key associated with the value you want to retrieve. To retrieve a multi-valued setting, use the GetValues() method, which returns an array of Objects containing the settings. If you use the GetValue() method to retrieve a multi-valued setting, the method returns all the values as a single concatenated comma-delimited string.

Similarly, to create a single-valued setting, use the AddValue() method; to create a multi-valued setting, use the AddValues() method, passing an Object array for the newValues() parameter. You can also create a multi-valued setting by passing a comma-separated array of strings to the AddValue() method.

Remember that XML is case-sensitive, so if the configuration file contains a key named "someKey", calling GetValue("somekey") does not return the correct result. Because the class can throw exceptions due to conversion problems, you should use Try/Catch when you're not sure that the data you pass or request can be converted to or from the specified type. However, I've minimized other types of exceptions; the class throws an exception only if the configuration file doesn't exist when the application first loads, or during the Replace() method, if the key or the value you're attempting to replace doesn't exist.

There's not enough room in this article to show examples of every call to the AppSettingsManager class, but the downloadable sample code contains a ConfigurationAlterationsVB project that lets you test the AppSettingsManager methods and properties, and see the changes made to the section of the configuration file.

Creating Custom Configuration Elements
Windows Forms configuration files don't load properly if you misspell the <configuration> or the <appSettings> tags, or if you add unrecognized tags to those sections. However, you're perfectly free to add whatever you like as child nodes of the <appSettings><add> child tags—in other words, the sky's the limit.

For example, the following XML causes an error when you try to obtain a value using the System.Configuration.ConfigurationSettings.AppSettings.Get(key) method.

<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="v1" value="Value 1" /> <add key="v2" value="Value 2" /> <mySettings>test</mySettings> </appSettings> </configuration>

In the configuration file shown above, the unrecognized tag causes an error when you attempt to read a configuration setting. However, by placing custom tags inside a (recognized) <add> tag, you can create any XML you like. The following configuration file does not cause an error:

<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="v1" value="Value 1" /> <add key="v2" value="Value 2" /> <add key="vx" value="nothing"> <mySettings>test</mySettings> </add> <add key="v4" value="Value 4" /> </appSettings> </configuration>

Despite the child <mySettings> tag, the framework happily reads the second version and simply ignores the extra child tags. If you use the framework classes to query the value of the "vx" key, it returns "nothing," and no error occurs.

So, you can see that although the sample code for this article deals only with modifying the standard <add key="aKey" value="aValue" /> child tags of the <appSettings> element, you can create your own version of the AppSettingsManager class to read and write any XML markup that meets your needs.

Russell Jones is DevX's Executive Editor. He's a former reptile keeper and professional musician who now composes computer applications. His most recent books are Mastering ASP.NET with VB.NET and Mastering ASP.NET with Visual C# (both published by Sybex). Reach him by e-mail .
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date