Reputation: 15325
As far as I understand, I should use System.Configuration.ConfigurationManager
to read app settings in desktop applications, and System.Web.Configuration.WebConfigurationManager
to do the same thing in web applications. This is what MSDN says.
I am developing a component that will be deployed to both desktops and web servers, so I am trying to make configuration logic independent of the platform. To my surprise, I discovered by accident that app settings are read correctly by using System.Configuration.ConfigurationManager
on a web server.
I have two related questions:
System.Configuration.ConfigurationManager
on web applications?Thanks.
Upvotes: 3
Views: 853
Reputation: 124686
MSDN says that WebConfigurationManager
is the preferred way to work with configuration files in web applications.
In web applications, configuration can be inherited from a web.config file in a parent virtual directory: WebConfigurationManager
will handle such inherited settings properly, which I believe isn't the case for ConfigurationManager
.
Of course, if your application does not inherit such settings (e.g. is always at the root of a web site), this is not relevant to you.
Upvotes: 4
Reputation: 4220
The System.Web.Configuration.WebConfigurationManager.AppSettings
and System.Web.Configuration.WebConfigurationManager.ConnectionStrings
internally uses the same System.Configuration.ConfigurationManager
- so there is no difference.
MSDN says:
These methods perform read-only operations, use a single cached instance of the configuration, and are multithread aware.
The other methods of the System.Web.Configuration.WebConfigurationManager
are related to configuration of ASP.NET. I consider the System.Web.Configuration.WebConfigurationManager
as extension of ConfigurationManager
, or even adaptation for the web ecosystem.
So, you should use the generic System.Configuration.ConfigurationManager
.
Upvotes: 1