Error 4 The name 'ConfigurationManager' does not exist in the current context
Are you trying to use the class ConfigurationManager in your .NET desktop application and getting the error "Error 4 The name 'ConfigurationManager' does not exist in the current context"? Read this post to find how to resolve this error and how to use ConfigurationManager class in place of old ConfigurationSettings class.
I have been using the old ConfigurationSettings in many of my old .NET applications to read configuration data from the app.config file. I knew this class was outdated and VS.NET kept reminding me about replacing it with the new ConfigurationManager class. Since the old code worked well, I did not feel the need to replace it with the new class. So, I left all my old code as it is.
Today I was developing a Google+ API library for .NET and had to read few configuration from the config file. I thought I would use the new ConfigurationManager class to read the data from the app.config file instead of sticking to the old class.
I typed the class ConfigurationManager and expected the intellisense to help me how to proceed. Surprisingly, intellisense complained it could not recognize and it suggested me to generate a new class with that name. I double checked and confirmed I am using the right class name. Everything looked alright and still Visual Studio could not recognize the class.
A quick look at MSDN showed me the System assembly where this class is defined. I looked at the project references and found this assembly is not referenced in the project references. Adding a reference to System.configuration.dll solved this problem.
Are you also getting the error Error 4 The name 'ConfigurationManager' does not exist in the current context?
Try the following steps:
Once you add a reference to System.Configuration, you should be able to use ConfigurationManager class, which is part of the System.Configuration namespace. So, remember to import the namespace as shown below:
C# Example:
using System.Configuration;
VB.NET Example:
imports System.Configuration
Once you add a reference to the System.Configuration assemly and use the namespace as shown above, you should be able to use the ConfigurationManager without any error. Here is an example of how to use it to read configuration settings from app settings file.
Example:
string customerId = ConfigurationManager.AppSettings["CustomerId"];
Why micosoft dont provide ConfigurationManager class in windows project. but it is available by default in asp.net