Search code examples
c#.netconfigurationmanager

c# Appsettings giving error: The name 'ConfigurationManager' does not exist in the current context


At the top of my program I have the following :

       using System.Configuration;

Within my code, I have the following:

int  CompanyID = Convert.ToInt32(ConfigurationManager.AppSettings["CompanyId"]
    .ToString());

I am getting the following error though :

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

I am not sure what I am missing.


Solution

  • To expand a bit, you will need to add a reference to System.Configuration.dll to get this to work. It's kind of misleading because the System.Configuration namespace also exists inside the base System.dll, and holds some far lesser used objects like SettingsContext. As a result, it seems like it really ought to work, but it doesn't. It is really confusing, and currently one of those obtuse gotchas in the .NET framework.

    Fortunately, System.Configuration.dll is in the .NET base framework, so you only need to add a reference by right-clicking on the References folder in your project, clicking Add Reference, and then finding System.Configuration under the .NET tab.

    After it has been imported into your project, don't forget to add using System.Configuration to the top of the code file you intend to use ConfigurationManager in.