I'm working on a C# class library that needs to be able to read settings from the web.config
or app.config
file (depending on whether the DLL is referenced from an ASP.NET web application or a Windows Forms application).
I've found that
ConfigurationSettings.AppSettings.Get("MySetting")
works, but that code has been marked as deprecated by Microsoft.
I've read that I should be using:
ConfigurationManager.AppSettings["MySetting"]
However, the System.Configuration.ConfigurationManager
class doesn't seem to be available from a C# Class Library project.
Does anyone know what the best way to do this is?
web.config
is used with web applications.web.config
by default has several configurations required for the web application. You can have aweb.config
for each folder under your web application.app.config
is used for windows applications. When you build the application in vs.net, it will be automatically renamed to<appname>.exe.config
and this file has to be delivered along with your application.You can use the same method to call the
app settings
values from both config files :for ex:
I had the same problem, just read them this way:
You'll need to add a reference to
System.Configuration
in your project's references folder.You should definitely be using the
ConfigurationManager
over the obsoleteConfigurationSettings
.As i found best approach to access app settings variables in systematic way by making a wrapper class over System.Configuration as below
Now we can access needed settings variables by hard coded names using another class as below
I have been trying to find a fix for this same issue for a couple days now. I was able to resolve this by adding a key within the appsettings tag in the web.config. This should override the .dll when using the helper.