Fredrik Normén's Blog - NSQUARED²
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Microsoft Most Valuable Professional
     .Net Framework - ASP.Net - Architecture - Development
NOTE: This list of posts will only list the 15 latest posts, to see the rest, select from the Archives located in the menu to the left. The RSS will only list the 10 lastest posts.
The new ConfigurationManager class

Category:  ASP.Net 2.0

With the previous beta versions of .Net 2.0, you could use the ConfigurationSettings to get the connection string, like this:

 

ConfigurationSettings.ConnectionStrings["myConKey"].ConnectionString

 

There is now a new class that you can use to handle most of the configuration, ConfigurationManager.

 

The ConfigurationManager class has methods and properties to let you open configuration files and retrieve configuration sections. The ConfigurationManager is the preferred class to use when you work with the configuration files, all other method are deprecated.

 

To get the ConnectionStrings from the configuration file you now write the following code:

 

ConfigurationManager.ConnectionStrings["myConKey"].ConnectionString

 

The following list has the properties and methods of the ConfigurationManager class:

Properties:

 

AppSettings

 

Gets the AppSettings section from the configuration file.

 

Now:

 

ConfigurationManager.AppSettings["MyKey"]

 

Before:

 

//This will still work for backward compatibility

ConfigurationSettings.AppSettings["MyKey"]

 

ConnectionStrings

 

Gets the ConnectionStrings section form the configuration file.

 

Now:

 

ConfigurationManager.ConnectionStrings["myConKey"].ConnectionString

 

Before in previous version of .Net 2.0:

 

ConfigurationSettings.ConnectionSteings["myConKey"].ConnectionString

 

Note: The ConnectionStrings property do not exists in ASP.Net 1.x. It’s a new property added to the .Net 2.0 framework.

 

Methods:

 

GetSection

 

GetSection will get a specific configuration section from the current application’s default configuration.

 

Now:

 

ConfigurationManager.GetSection("mySection")

 

Before:

 

//This still work for backward compatibility

ConfigurationSettings.GetConfig("mySection") 

 

GetWebAppSection

 

With the GetWebAppSection you can get the specified section from the web applications configuration file.

 

Note: This method retrieves the specified configuration section from the configuration file located at the root folder of your Web application. If you want to retrieve the configuration section from the current Web application directory use the GetSection method.

 

RefreshSection

 

RefreshSection will refresh the specified section so next time its requested, it will be re-read from disk.

 

With the following method, you will have the ability to programmatically change the settings within the configuration files. Those methods will return a Configuration class (The Configuration class has properties to get sections from the configuration file you want to edit, and by using the Save or SaveAs method, you can save the settings):

 

OpenMachineConfiguration

 

With the OpenMachineConfiguration, you configure the machine.config file.

 

OpenMappedMachineConfiguration

 

With the OpenMappedMachineConfiguration, you can open a specified configuration file.

 

OpenExeConfiguration

 

With the OpenExeConfiguration, you can open the client’s configuration file.

 

OpenMappedExeConfiguration

 

With the OpenMappedExeConfiguration, you can open the specified client configuration.

 

OpenWebConfiguration

 

With the OpenWebConfiguration, you can open the web applications configuration file.

 

OpenMappedWebConfiguration

 

With the OpenMappedWebConfiguration, you can open a specified web configuration file.

Posted: Wednesday, December 15, 2004 - 08:51 GMT+1    Print     E-mail    Comments (9)
   fredrik.nsquared2.com - 2007