What's the difference between the WebConfigurationManager and the ConfigurationManager?

C#.Netasp.netConfigurationmanagerWebconfigurationmanager

C# Problem Overview


What's the difference between the WebConfigurationManager and the ConfigurationManager?

When should I use one over the other?

UPDATED

I just looked at the WebConfigurationManager, and for some reason, you can't access the connection strings as you do in the ConfigurationManager (like an array). Can anyone tell me why MS made it like this? It seems to be a pain to get the connection string you need using the WebConfigurationManager.

UPDATED AGAIN with CAVEAT!

If you don't have a reference to the System.Configuration namespace added to your project, then Visual Studio will show an error when you try and access the WebConfigurationManager.ConnectionStrings like an array!

C# Solutions


Solution 1 - C#

WebConfigurationManger knows how to deal with configuration inheritance within a web application. As you know, there could be several web.config files in one applicaion - one in the root of the site and any number in subdirectories. You can pass path to the GetSection() method to get possible overridden config.

If we'd looke at WebConfigurationManager with Reflector then things are clear:

public static object GetSection(string sectionName)
{
    ...
    return ConfigurationManager.GetSection(sectionName);
}

public static object GetSection(string sectionName, string path)
{
    ...
    return HttpConfigurationSystem.GetSection(sectionName, path);
}

 

Solution 2 - C#

WebConfigurationManager is made specifically for ASP.NET applications.

WebConfigurationManager provides additional methods to load configuration files applicable to Web applications.

ConfigurationManager provides also methods to load configuration files applicable to ".exe" applications.

I’d suggest taking a look at WebConfigurationManager and see if it provides you with anything you simply cannot do with ConfigurationManager and use it instead, otherwise using ConfigurationManager will make it far easier to have your code be used seamlessly between web and desktop aps.

Solution 3 - C#

Although WebConfigurationManager is located in the System.Web assembly the ConnectionStringSettingsCollection that it returns is located in System.Configuration.

If you are getting the error

> Cannot apply indexing with [] to an expression of type > 'System.Configuration.ConnectionStringSettingsCollection'

while trying to access the array index...

WebConfigurationManager.ConnectionStrings["Name"].ConnectionString

make sure you have a reference to assembly System.Configuration

Solution 4 - C#

Not sure what you mean about the connection strings.

Calling WebConfigurationManager.ConnectionStrings returns a System.Configuration.ConnectionStringSettingsCollection, which is the same as you would get if you called ConfigurationManager.ConnectionStrings.

Otherwise, as XOR says, it's designed to handle multiple hierarchical web.configs, combining them as required as you move around the folders in an application.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn BView Question on Stackoverflow
Solution 1 - C#XORView Answer on Stackoverflow
Solution 2 - C#Konstantin TarkusView Answer on Stackoverflow
Solution 3 - C#WilliamView Answer on Stackoverflow
Solution 4 - C#Zhaph - Ben DuguidView Answer on Stackoverflow