Hack 48. Extend the Application Configuration File![]() configuration sections to your app.config file. Custom configuration sections are a valuable tool for storing application-specific settings. With .NET, you can extend the normal configuration files and include your own custom configuration sections. Normally, creating your own custom configuration section involves writing classes and a configuration section handler to access your custom settings. There is an easier and much quicker way to handle the creation of a custom configuration section and all the code that you need to work with it.The open source custom tool, ConfigBuilder, can be used to automatically generate configuration section handlers, as well as custom objects that can be used as data containers for the settings.To use the ConfigBuilder custom tool, you will need to download and run the installation file from http://workspaces.gotdotnet.com/configbuilder. 6.6.1. Building the Configuration TemplateTo start using ConfigBuilder, you first need to add a new XML file to your project. This XML file is called the configuration template file and will be used to specify the format of your configuration section. This file will then used to generate the code to access this configuration section. The configuration template can be named whatever you like and should be in the location where you want the code to be stored. (The code will appear underneath this XML file in the Solution Explorer, exactly like other custom tool-generated code.)The first step to building your configuration template file is to add a root element named configuration, the same as a normal configuration file. Next you will need to declare your custom sections by creating an element for the section and then any number of attributes. The name of the section and the name of the attributes should be the same as the names that will be used in the configuration section, but instead of specifying values, you will need to specify the type that this attribute expects. To specify the type, you will need to use C# type keywords such as string, int, short, and so forth.Here is an example of declaring a custom section called DatabaseSettings: <configuration>In this file, I am creating a single element called DatabaseSettings that has three different attributes; the first two are strings and the last is an integer.You can also define default values for attributes in the template file. In this example, suppose that I want to set a default value for the timeout attribute parameter. I can do this by simply adding a colon after the type and then specifying my default value. My template file would now look like this: <configuration>By declaring a default value, the attribute is no longer required. If the timeout attribute is omitted in the configuration section for this application config file, the code will simply assume the value is 60. (When declaring default values for strings, you must use [Empty] to signify an empty string.)You can also specify the number of times a section can appear, create section groups, and specify comments to be included in the generated code. Explanations of these features can all be found in ConfigBuilder's documentation. 6.6.2. Running the ConfigBuilderTo execute the template file and generate the code, you need to specify ConfigBuilder as the custom tool for this file. This is done by simply entering ConfigBuilder into the Custom Tool field in the .XML file's property window as shown in Figure 6-7. Figure 6-7. ConfigBuilder set as the custom tool![]() will be run every time the file is saved, regenerating the code based on any changes to the template file.After you specify ConfigBuilder as the custom tool, a plus sign will appear next to the .XML file. The plus sign can be expanded to show the code that has been generated based on the template. Here is a look at the code generated based on the template from the preceding example (I have removed comments for the sake of brevity): namespace ConfigBuilderExample 6.6.3. Using the CodeTo use this configuration section in your configuration file, you need to declare your custom section in the configSections section of the configuration file for your application: <configSections>ConfigBuilderExample is the name of my namespace in this example. Next, you need to actually add your configuration section to the application configuration file: <configuration>Then you can read these values from the configuration file using the following code anywhere in your application: DatabaseSettingsConfig config = (DatabaseSettingsConfig)The first line of this code gets an instance of the DatabaseSettingsConfig class from the configuration file. You can then access any of the configuration settings by simply accessing the properties of this class. In this example, the value of the server element is shown to the user through a message box.This custom tool is a great time-saver and makes it extremely easy to create and use custom configuration sections. |