RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Working with Windows Forms Configuration Files in .NET 2.0 and Visual Studio 2005

The newest version of the .NET framework extends strong data typing to configuration files, adds scope for settings, a GUI-based property editor, and drops the read-only restriction for configuration classes.

mong the many new features in the forthcoming .NET 2.0 are the revamped System.Configuration namespace and the Visual Studio 2005 configuration editor. The new classes raise configuration for both desktop and Web applications to a new level of sophistication compared to prior implementations. The sheer size of the changes could fill a small reference text, so this article focuses on a very simple desktop application whose only purpose is to display and modify content from its own configuration file. To run the sample application you need to download the Visual Studio 2005 public beta. I used the February 2005 Professional Edition for all the screenshots and code builds.

Some New Features
Two of the most important new configuration file features are strong data typing for type safety and a separate and editable scope for user settings.

Type Safety
The previous version of the .NET Framework only allowed for string settings. This could create problems when reading settings directly into non-string variables, as seen in the code fragment below:

   int maxConnections = 

If the configuration content wasn't a string, but instead represented some other data type, such as a Boolean, integer, or more complex data type, you had to write custom code to cast the string value or create and populate the appropriate object. In contrast, the new API has specific classes that handle all the basic data types and interfaces for implementing custom serializers. In addition the framework has built-in handlers for some of the more commonly used programmatic constructs such as data source connection strings and URLs.

The new API delineates separate application and user settings using a concept called scopes. You use the Application scope for settings specific to the application such as connection strings and other values that drive the application itself, and are not prone to change. The User scope is for storing user-configurable application values such as the last screen position and the most recently used documents. Most importantly, the User scope stores default values for each setting. When the user changes these default values by using the application, the configuration file stores these updated values in a separate location. This is important because it protects the integrity of the application's configuration file and keeps user-specific data with the user's system profile. The configuration framework loads user-specific settings automatically in the background without any additional developer effort.

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