views:

2406

answers:

5

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?

A: 

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

+2  A: 

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

kpollock
my first thought just delete the question before anyone notices! ;)
kenny
it won't let me... to manhy answers. uess I'l just have to live with it!
kpollock
Dont worry, I just did the exact same thing and this answer helped me realize my own screwup ;)
omglolbah
+2  A: 

I think you need to implicitly refer to System.Configuration assembly.

TheVillageIdiot
+9  A: 

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.

+1  A: 

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;