Coder Perfect

In.NET, you can read settings from app.config or web.config.


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

Solution #1

For example, consider the following app.config file:

<?xml version="1.0" encoding="utf-8" ?>
    <add key="countoffiles" value="7" />
    <add key="logfilelocation" value="abc.txt" />

Using the code described below, you may read the above application settings:

using System.Configuration;

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

Solution #2

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

Solution #3

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

Solution #4

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

Solution #5

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