<!-- Article image -->
<!-- Add the rest of your HTML here -->
Since I discovered configuration sections and configuration section handlers, I've been using custom configuration sections to hold a variety of runtime required settings. What I've wanted for some time now was an easy way to edit these sections, either for simple application setting changes, or during deployment as a custom installation action. In this article I'll describe the approach and logic to the development of my own configuration editor. Download the source code and run the project to see the application in action.
The first example of a custom configuration handler I came across was in the sample Duamish7 application that ships with the .NET Framework SDK. I confess my first reaction was 'oh my god'. It looked like a lot of code to implement the equivalent of a few registry settings. Looking back I can see why I was initially skeptical about their value. Even without using a custom configuration section handler the .NET Framework Class Library supplies a simple NameValue section reading class in
System.Configuration.ConfigurationSettings.AppSettings - for quick and easy reading of any setting in the <appSettings> section. It wasn't until recently that I discovered the true value of a custom configuration handler. By handling the section yourself you are no longer limited to simple key value pair attributes. In fact you can build your own XML structure altogether and do what ever you want with this when the node for your section is passed to the handler. There are a lot of excellent resources out there that describe how to implement your own custom configuration section handler and so that's not the topic here, although I'll include several links to them below. What you will see in this application however is the value of handling a section yourself, in a simple way. I've extended the name value pair keys to include an additional XML attribute for the setting, the 'description' attribute which you can see in action in the screen shot above.
PropertyGrid control. It turns out that the
PropertyGrid is perfectly suited for this application. I wanted a two column grid with the first column readonly, and the second column editable and the whole thing divided nicely into categorized sections. I've seen at least one other attempt at creating a Configuration file editor using the
DataGrid and a
DataSet however it just didn't seem as elegant a solution as using the PropertyGrid. If you're wondering where the
PropertyGrid control comes from you'll be pleased to know you already have it, it's free to use as a .NET control, you just need to add it to the toolbar in Visual Studio.
Dynamic Properties. Having chosen the tools and controls I was faced with a problem - how to effectively de-serialize the configuration sections I was interested in, creating an object that could be used for the
SelectedObject property. The
PropertyGrid is an impressive control. It will automatically display all the properties of a class, along with property values which can be edited. How you create the object and what you do with it when you're finished changing values is the 'interesting' part. There is a LOT of customization options that will determine how values are displayed in the property grid. Again there are links below to the MSDN resources that provide excellent descriptions on how to use the
Full Credit. As often happens it was while trolling through the newsgroups in search of a solution to the problem of how I was going to create an object at runtime, I came across a posting that described the world of
PropertyDescriptor classes. Thank you Venu Madhav (firstname.lastname@example.org) for pointing me in the right direction.
CustomClass in this application is adapted from Venu's
CustomClass and there are notes in the application that describe the changes.
Using the code
To use the application - open the Visual Studio project and compile the application as a Windows Application - a .Exe file. Then change the project properties and Output Type to Class Library and compile again. This will create a .dll assembly. To use the Configuration Editor in your own Windows application - add a reference to the compiled .dll file and add the following two lines of codes to your application (under a menu called Settings for example). That's all there is to it. The application will attempt to find the .Config file if there is one present and automatically load it.
private void mnuSettings_Click(object sender, System.EventArgs e)
DotBits.Configuration.ConfigEditor c = new
If you want to include the .Exe in your Setup Project as a Custom Action during installation simply include the ConfigEditor.Exe in the Setup Project as a required file. Create a Custom Action that points to the .Exe (and remember to change the Custom Actions InstallerClass property to false).
Note: There is a sample App.Config file in the project. You can run the project as soon as you unzip it to see what the application looks like - however you might be tricked into thinking that your changes to the .Config file are not being saved. Remember that Visual Studio will copy the App.Config file into the compiled directory every time the project is run - and so any setting changes you make while running the project will be lost when you run the project again.
Custom Configuration Sections and Handlers
Application Configuration Files Explained (Adventures in .NET)
Declaring Custom Configuration Sections (.NET Framework Developer's Guide)
Custom Element for NameValueSectionHandler and DictionarySectionHandler
The PropertyGrid Control
Getting the Most out of the .Net Framework PropertyGrid Control
Make your Components Really RAD with Visual Studio .Net Property Browser
- First post 22 Feb 2004
- Updated source code 2 Mar 2004<!------------------------------- That's it! --------------------------->