Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Configuration.ConfigurationManager not available?

People also ask

What is ConfigurationManager configuration?

The ConfigurationManager class enables you to access machine, application, and user configuration information. This class replaces the ConfigurationSettings class, which is deprecated. For web applications, use the WebConfigurationManager class.

How do I add a reference to System configuration ConfigurationManager?

Right click the project and choose add reference, browse "Assemblies" and tick the System. Configuration assembly.

Does ConfigurationManager work in .NET core?

ConfigurationManager was added to support ASP.NET Core's new WebApplication model, used for simplifying the ASP.NET Core startup code.


Although the using System.Configuration; command is automatically generated in the using section, for some reason the actual reference is not set.

Go into add reference, .Net tab, and choose System.Configuration.

ConfigurationManager will now be resolved.

If you go to the project where the exact same setup works just fine and look at the references, you will see a reference to System.Configuration.


To answer the question (not that it hasn't been answered already 5+ times) is to add System.Configuration as a reference to your project.

However what I would really like to highlight is that on many occasion I have added the System.Configuration.dll to my project's references, but for whatever special reason sometimes ConfigurationManager still won't show up in intellisense even after adding the reference to System.Configuration. Even if I remove the reference and add it again.

The very simple solution to this problem is to:

  1. Add the reference to System.Configuration.dll to your project
  2. Save your files
  3. Save your Solution
  4. Close the instance of Visual Studio that is giving you a hard time
  5. Re-open your solution

This simple exercise will get Visual Studio to behave again and stop telling you that you did not add your reference to System.Configuration. This exercise usually helps me with all inexplicable Visual Studio behaviors.

I have had this happen to me in both VS2008 and VS2010 multiple times and it works every time.


urgh - PICNIC error. Added ref to the wrong project in the solution...


For anyone who switches back and forth between developing ASP.NET WebForms and WinForms, this tip may come in handy.

If you are developing in a C# WinForms project, you will find that attempting to use a ConfigurationManager to get at your app.config settings will result in the following error:

The name 'ConfigurationManager' does not exist in the current context

Since this is included by default in ASP.NET projects, this may come as a surprise. Just simply right-click on the "References" node in your project and look on the ".NET" tab. Scroll down and you should find System.Configuration. Add this to your project and you should be up and running.

Adding a Reference to System.Configuration

Provided you have already added System.Configuration to the using section at the top of your code, you should now be able to use config settings (such as connection strings) with code such as the following:

con.ConnectionString = ConfigurationManager.ConnectionStrings[sConnection].ConnectionString;

From the MSDN documentation -

To use the ConfigurationManager class, your project must reference the System.Configuration assembly. By default, some project templates, like Console Application, do not reference this assembly so you must manually reference it.

https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx