Database Search Solution
(New Version) Search Control
Free ASP.NET Controls
ASP.NET Media Player Control
Flash Video Player Control
ASP.NET Telecommute Jobs
Free IP Location Lookup
Test .Net Regular Expressions
CSS/Table/DIV Page Layouts
Article Sites Master List
.NET Windows Forms
General .NET Framework
Accepting Credit Cards
Configuring Windows Application
In this article we will discuss about configuration files, types of configuration files and their internal schema and settings.
Configuration files are XML files that contain configuration settings for applications. Configuration files are used to change application settings without recompiling the applications. They can also be used to set machine policies that affect how applications run on a computer. Configuration files can be modified whenever required.
Format of a Configuration file
Configuration files contain a hierarchy of elements that specify configuration information. A sample configuration file is given below:
The <configuration> element is the root element in every configuration file used by the common language runtime and .NET Framework applications.
A configuration file uses tags to mark the beginning and end of an element. For example, the <runtime> element has a beginning tag specified as <runtime> and an end tag as </runtime>. An empty element however has a start tag but no end tag. Within the beginning and ending tags of an element there may be any number of child elements. The start tag of an element may contain one or more attributes. An attribute is specified using a name/pair. You make configuration settings by specifying appropriate values for attributes in an element's start tag.
Types of Configuration Files
There are three types of configuration file:
- Application configuration file
Application Configuration Files
They contain configuration settings specific to applications. These files provide a way of overriding the metadata in assemblies without having to rebuild the application.
When you execute an application, the Common Language Runtime (CLR) searches for dependent files, e.g. assemblies and resource files, that the application needs for execution. This process is also known as probing. During the probing process, the CLR searches for the application configuration file and examine it to determine the settings contained in it. The probing behavior is then modified depending on the configuration settings. For example, settings in the application configuration file can redirect an application to look for an assembly in a different folder. The new assembly however, must be built with the same key pair that the application expects.
The name of an application configuration file is the name of the application with a .config extension. An application called myapp.exe will have configuration file called myapp.exe.config. Also, this file will be located in the same directory as myapp.exe.
Machine Configuration Files
Machine Configuration Files include settings that apply to an entire computer. The name of machine configuration file is machine.config. The location of this file is:
Machine Configuration Files are useful when a component is used by multiple applications. In that case, it is easier to put the settings for that component in a single machine configuration file rather than creating separate application configuration files for different applications. By default the settings in the machine configuration files, override the settings in the application configuration files.
Security Configuration Files
Security configuration files contain information about permission sets and code group hierarchy. Theses files contain information related to code access security system. You can configure these files to modify the security policy. However, manual editing of security configuration files is not recommended. Rather you can use the .NET Framework Configuration tool (Mscorcfg.msc) or the Code Access Security Policy tool (Caspol.exe) to make these changes. Using these tools ensures that policy changes do not corrupt the security configuration files.
One can change the way an application runs by changing the application settings in the application configuration file. For this, you need to understand its elements.
Elements in an Application Configuration File:
Some configuration elements include attributes that provide additional details to the runtime. The following table lists these attributes:
Working with Application Configuration Files
Some areas where application configuration files can be useful are given below:
- Specifying the runtime version
Specifying the Runtime Version
This can be done by using the <supportedRuntime> element. For example:
Specifying the concurrent garbage collection
Specifying the location of an assembly
The location of an assembly can be specified using <codeBase> or <probing> element
The <codeBase> element specifies where the runtime can find a strong named assembly.
The runtime locates assemblies that do not have a code base by probing. The <probing> element specifies subdirectories of the application's base directory that the runtime should search when locating an assembly.
Redirecting assembly versions
When you build an application against a specific version of a strong-named assembly, the application uses that version of the assembly at run time. However, at times you want the application to run against a newer version of an assembly. You can use the <bindingRedirect> element to redirect one version of any assembly to another. For example:
Creating a publisher policy
When a component vendor releases a new version of an assembly, the vendor can include a publisher policy so applications that use the old version now use the new version.
You can use the <publisherPolicy> element to specify whether the common language runtime applies publisher policy.
You can specify whether to apply publisher policy in the application's configuration file for either a specific or all assemblies the publisher uses.
1. To specify whether to apply publisher policy for a particular assembly, put the <publisherPolicy> element in the <dependentAssembly> element.
2. To specify whether to apply publisher policy to all assemblies the application uses, put the <publisherPolicy> element in the <assemblyBinding> element.
Till now we have discussed configuration settings that the common language runtime reads when an application is executed.
A configuration file can also contain information that the application reads at run time. You can specify this information in configuration files by using configuration sections.
The .NET Framework provides several predefined configuration sections (e.g. <appSettings> and developers can also create custom configuration sections.
Configuration sections have two parts:
1. Configuration section declaration.
You can put Configuration section declarations and configuration setting in the machine Configuration file or in the application Configuration file.
Configuration Settings are read by section handlers at run time. Section handlers are classes that implement the IconConfigurationSectionHandler interface.
A Section handler first reads settings in the machine Configuration file and then in the application Configuration file. Depending on the section handler, either the settings in the application file override the settings in the machine file or the settings form both are merged.
The .NET framework uses the following section handler.
Configuration Section declarations are made up in the <configSections> element. A new configuration section is created by declaring it in a <section> element inside the <configSections> element. The <section> element has two prorperties:
Name: name of the element that contains the information the section handler reads.
Now that we have discussed the structure of a configuration section, let us discuss how to use predefined <appSettings> configuration section and how to create custom configuration section.
The following example shows the declaration of the appSettings section as it appears in the machine configuration file.
The following example shows how to make an application setting in a configuration file using the predefined appSettings section:
Application settings defined in the <appSettings> section can be accessed by using the ConfigurationSettings.AppSettings property. The following example shows how to retrieve the application name defined in the above configuration file:
Custom Configuration Sections
You can also declare configuration sections of your own. The syntax of configuration setting depends on the section handler that is associated with configuration section.
For example, to declare a custom section that uses the SingleTagSectionHandler class, the syntax is:
You can use the static method ConfigurationSettings.GetConfig in the System.Configuration namespace to access configuration settings from an application.
In the above code, the ConfigurationSettings.GetConfig method returns a value of type Object. You must cast Object returned from ConfigurationSettings.GetConfig to an IDictionary object type, which represents a collection of key-value pairs.
The set of key-value pairs is stored in the variable values_table. Each value in values_table must be converted to a String object.
comments powered by Disqus