Reputation: 3989
In a VS2005 C# project I have added a reference to System.configuration. In the object browser, I can see the System.Configuration.ConfigurationManager. In Intellisense System.Configuration only has the old ConfigurationSettings, and not ConfigurationManager.
My code System.Configuration.ConfigurationManager.AppSettings["MySetting"]
is highlighted as a syntax error and does not compile.
In a different project, the exact same setup works just fine... any clues as to what is going on?
Upvotes: 57
Views: 89829
Reputation: 3863
wow - this was a hard one for me to resolve; in the end, I found that my vb.net program would only compile if included the reference (as expected) and removed the "Imports System.Net" statement (as I did not expect) from my module.
Upvotes: 0
Reputation: 51
Upvotes: 5
Reputation: 7407
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
Upvotes: 4
Reputation:
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.
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;
Upvotes: 9
Reputation: 4285
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:
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.
Upvotes: 16
Reputation:
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.
Upvotes: 132
Reputation: 40527
I think you need to implicitly refer to System.Configuration assembly.
Upvotes: 3
Reputation: 3989
urgh - PICNIC error. Added ref to the wrong project in the solution...
Upvotes: 11
It's just a guess, but maybe you should check if your project is using at least .NET framework 2.0. ConfigurationManager class is availvable since .NET 2.0 as dfescribed here: link on msdn
Upvotes: 0