Configuration File Format Configuration

125 System.Text Types for manipulating text and strings. Note in particular the StringBuilder type. When strings are built from smaller parts, the methods on the StringBuilder class are more efficient than similar methods on the String class. This is because the instances of the String class cant be modified in place; every time a change is made to a String object, a new String object is actually created to hold the new value. In contrast, the methods in StringBuilder that modify the string actually modify the string in place. System.Threading Support for multithreaded programming. System.Timers Provides the Timer class, which can generate an event at predetermined intervals. This addresses one of the limitations of the Visual Basic 6 Timer control: it had to be hosted in a container and therefore could be used only in an application with a user interface. System.Web Support for building web applications. The types in this namespace constitute Web Forms and ASP.NET. System.Windows.Forms Support for building GUI fat client applications. The types in this namespace constitute Windows Forms. System.Xml Support for parsing, generating, transmitting, and receiving XML.

3.11 Configuration

System and application configuration is managed by XML files with a .config extension. Configuration files exist at both the machine and application level. There is a single machine-level configuration file, located at runtime_install_path \CONFIG\machine.config. For example, C:\WINNT\Microsoft.NET\Framework\v1.0.2914\CONFIG\machine.config. Application-configuration files are optional. When they exist, they reside in the applications root folder and are named application_file_name .config. For example, myApplication.exe.config. Web application- configuration files are always named web.config. They can exist in the web applications root folder and in subfolders of the application. Settings in subfolders configuration files apply only to pages retrieved from the same folder and its child folders and override settings from configuration files in higher-level folders. Configuration files should be used for all application-configuration information; the Windows Registry should no longer be used for application settings.

3.11.1 Configuration File Format

Configuration files are XML documents, where the root element is configuration . For example: 126 ?xml version=1.0 encoding=UTF-8? configuration -- More stuff goes in here. -- configuration To be as flexible as possible, .NET configuration files use a scheme in which the application developer can decide on the names of the subelements within the configuration element. This is done using the configSections , section , and sectionGroup elements. Ex am ple 3- 2 shows how this is done using the configSections and section elements; the sectionGroup element is discussed later in this section. Example 3-2. Defining a section in a configuration file ?xml version=1.0 encoding=UTF-8? configuration configSections section name=mySectionName type=System.Configuration.SingleTagSectionHandler configSections mySectionName someSetting=SomeValue anotherSetting=AnotherValue configuration The name attribute of the section element specifies the name of an element that will or could appear later in the file. The type attribute specifies the name of a configuration section handler, which is a class that knows how to read an XML section thats formatted in a particular way. The .NET Framework provides stock configuration section handlers notably the SingleTagSectionHandler and the NameValueSectionHandler classes, both of which will be discussed later in this section, which are sufficient for the majority of cases. Although its beyond the scope of this book, a custom configuration section handler can be created by writing a class that implements the IConfigurationSectionHandler interface. The SingleTagSectionHandler configuration section handler reads XML sections that are of the form: sectionName key1Name=Value1 key2Name=Value2 etc... The element can contain any number of keyvalue pairs. The configuration section handler class is not used directly in code. To read information from an applications configuration file, use the GetConfig method in the ConfigurationSettings class defined in the System.Configuration namespace. The syntax of the GetConfig method is: Public Shared Function GetConfigByVal sectionName As String As Object Heres how the mechanism works an example will follow: 1. The application calls the GetConfig method, passing it the name of the configuration section that is to be read. 2. Internally, the GetConfig method instantiates the configuration section handler class that is appropriate for reading that section. Recall that it is the values found in the 127 configSections portion of the configuration file that identify the appropriate configuration section handler class to use. 3. The Create method of the configuration section handler is called and is passed the XML from the requested configuration section. 4. The configuration section handlers Create method returns an object containing the values read from the configuration section. 5. The object returned from the Create method is passed back to the caller of the GetConfig method. The type of object returned from GetConfig is determined by the specific configuration section handler that handles the given configuration section. The caller of the GetConfig method must have enough information about the configuration section handler to know how to use the object that is returned. Two stock configuration section handlers—and the objects they create—will be discussed in this section. Ex am ple 3- 3 shows how to read the configuration file shown in Ex am ple 3- 2 . To run this example, do the following: 1. Create a new directory for the application. 2. Save the code from Ex am ple 3- 3 into a file named ConfigurationTest.vb. 3. Compile the code with this command line: vbc ConfigurationTest.vb reference:System.dll The reference to the System assembly is required because the System assembly contains the definition of the ConfigurationSettings class. The compiler creates an executable file named ConfigurationTest.exe. 4. Save the configuration file from Ex am ple 3- 2 into a file named ConfigurationTest.exe.config. Run the executable from the command prompt. The application prints the configuration values to the command window. Example 3-3. Reading the configuration file shown in Ex a m ple 3 - 2 Imports System Imports System.Collections Imports System.Configuration Public Module SomeModule Public Sub Main Dim cfg As Hashtable Dim strSomeSetting As String Dim strAnotherSetting As String cfg = CTypeConfigurationSettings.GetConfigmySectionName, _ Hashtable If Not cfg Is Nothing Then strSomeSetting = CTypecfgsomeSetting, String strAnotherSetting = CTypecfganotherSetting, String End If Console.WriteLinestrSomeSetting Console.WriteLinestrAnotherSetting End Sub End Module 128 To read the configuration settings, the code in Ex am ple 3- 3 calls the GetConfig method of the ConfigurationSettings class. The SingleTagSectionHandler configuration section handler creates a Hashtable object defined in the System.Collections namespace to hold the keyvalue pairs found in the configuration file. That is why the code in Ex am ple 3- 3 calls the CType function to convert the reference returned by the GetConfig method to a Hashtable reference. After that is done, the code can do anything appropriate for a Hashtable object, including retrieving specific values by key as shown in Ex am ple 3- 3 or iterating through the Hashtable objects items. Also note that because Hashtable objects store values of type Object, the object references retrieved from the Hashtable have to be converted to the appropriate reference type, which in this case is String. The Visual Basic CStr function could have been used here, although in this case the Visual Basic CType function is called instead. The application does not specify the name of the configuration file in which to look for the configuration information. The system automatically looks in the application_file_name .config file found in the applications directory. If the requested section is not found in that file, the system automatically looks for it in the machine-configuration file. Another stock configuration section handler is the NameValueSectionHandler class. This handler also reads keyvalue pairs, but in a different format. Ex am ple 3- 4 is the same as Ex am ple 3- 2 , but rewritten to use NameValueSectionHandler. Example 3-4. Using the NameValueSectionHandler configuration section handler ?xml version=1.0 encoding=UTF-8? configuration configSections section name=mySectionName type=System.Configuration.NameValueSectionHandler configSections mySectionName add key=someSetting value=SomeValue add key=anotherSetting value=AnotherValue mySectionName configuration Ex am ple 3- 5 shows the code that reads this configuration section. Example 3-5. Reading the configuration file shown in Ex a m ple 3 - 4 Imports System Imports System.Collections.Specialized Imports System.Configuration Public Module SomeModule Public Sub Main Dim cfg As NameValueCollection Dim strSomeSetting As String Dim strAnotherSetting As String cfg = CTypeConfigurationSettings.GetConfigmySectionName, _ NameValueCollection If Not cfg Is Nothing Then 129 strSomeSetting = CTypecfgsomeSetting, String strAnotherSetting = CTypecfganotherSetting, String End If Console.WriteLinestrSomeSetting Console.WriteLinestrAnotherSetting End Sub End Module The main difference to note in Ex am ple 3- 5 is that the NameValueSectionHandler creates an object of type NameValueCollection defined in the System.Collections.Specialized namespace.

3.11.2 Configuration Section Groups