I’m developing a C# class library that requires the ability to read settings from the web.config or app.config files (depending on whether the DLL is referenced from an ASP.NET web application or a Windows Forms application).
I’ve found that
Although it works, Microsoft has designated the code as deprecated.
According to what I’ve read, I should use:
The System, on the other hand. Configuration. A C# Class Library project does not appear to have the ConfigurationManager class.
What is the most effective method for accomplishing this?
Asked by Russ Clark
For example, consider the following app.config file:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="countoffiles" value="7" /> <add key="logfilelocation" value="abc.txt" /> </appSettings> </configuration>
Using the code described below, you may read the above application settings:
It’s possible that you’ll need to include a reference to System as well. If your project doesn’t already have one, create one. The values can then be accessed as follows:
string configvalue1 = ConfigurationManager.AppSettings["countoffiles"]; string configvalue2 = ConfigurationManager.AppSettings["logfilelocation"];
Answered by Ram
You’ll need to include a System reference. Configuration in the references folder of your project.
You should always use the ConfigurationManager instead of the now-defunct ConfigurationSettings.
Answered by womp
The following will no longer work with.NET Framework 4.5 and 4.6:
string keyvalue = System.Configuration.ConfigurationManager.AppSettings["keyname"];
Now use Properties to get to the Setting class:
string keyvalue = Properties.Settings.Default.keyname;
For further information, see Managing Application Settings.
Answered by bsivel
Right click on your class library, and choose the “Add References” option from the Menu.
Select System.Configuration from the.NET tab. The System.Configuration DLL file would be included in your project.
Answered by Shiva
This is what I’m using, and it’s working well for me:
textBox1.Text = ConfigurationManager.AppSettings["Name"];
Answered by Pagey
Post is based on https://stackoverflow.com/questions/1189364/reading-settings-from-app-config-or-web-config-in-net