Configuration File Settings


The .NET Framework allows applications to store settings in XML configuration files. In many cases, these settings are known only to the applications themselves. For example, an application might wish to store a connection string that is used to connect to a database. However, as I've shown, these configuration files can also contain settings used to customize how the CLR behaves while running that application. In the previous section, I demonstrated how these configuration files can be used to specify an assembly search path, for example. It's likely that you've run across these configuration files already as you've built .NET Framework applications. For example, you've probably edited the web.config file to customize the way your ASP.NET application works.

An application's settings can be specified in one of a number of XML configuration files arranged in a hierarchy. At a minimum, settings can be supplied in either a configuration file that affects all applications on a machine or in a file specific to a particular application. Some scenarios support more extensive configuration systems. For example, ASP.NET allows a developer or administrator to provide settings at any level in the directory structure.

All application scenarios supported by the CLR include the notion of machine-wide configuration settings. These settings are stored in a file with a fixed name (machine.config) and location (%windir%\microsoft.net\framework\versionnumber Config) so the CLR knows exactly where to find the file. In contrast, the name and location of an application-level configuration file (if one exists) must be specified when an application domain in which to run the application is created. The association between the configuration file and the application is made using the ConfigurationFile or ConfigurationBytes properties of AppDomainSetup. For example, the default CLR host supports configuration files for executable files. These configuration files must be in the same directory as the executable and must be named exe name.exe.config, where exe name is the name of the executable being launched. When the default CLR host creates the domain in which to run the executable, it sets the ConfigurationFile property to the exe name.exe.config file as shown in the following example:

    AppDomainSetup adSetup = new AppDomainSetup();     adSetup.ApplicationBase = @"c:\MyApp";     adSetup.ConfigurationFile = @"c:\MyApp\MyApp.exe.config";     AppDomain ad = AppDomain.CreateDomain("MyApp", null, adSetup);

Other application scenarios work the same way. ASP.NET uses the same technique to associate a web.config file with a domain, for example.

The ConfigurationBytes property enables you to specify your configuration file as an array of bytes instead of by supplying the name of a configuration file on disk. The byte array you pass to ConfigurationBytes is simply the contents of your XML configuration file laid out in memory. Specifying a configuration file with ConfigurationBytes is convenient for scenarios in which you generate configuration information dynamically instead of storing the data ahead of time in a disk file.



    Customizing the Microsoft  .NET Framework Common Language Runtime
    Customizing the Microsoft .NET Framework Common Language Runtime
    ISBN: 735619883
    EAN: N/A
    Year: 2005
    Pages: 119

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net