6
var values = new NameValueCollection
{
    { "key", ConfigurationSettings.AppSettings["API-Key"].ToString() },
    { "image", Convert.ToBase64String(File.ReadAllBytes(photo.ToString())) }
};

What's the new way to use the app.config file?

Kelsey
  • 47,246
  • 16
  • 124
  • 162
Sergio Tapia
  • 40,006
  • 76
  • 183
  • 254
  • Possible duplicate of [The name 'ConfigurationManager' does not exist in the current context](http://stackoverflow.com/questions/1274852/the-name-configurationmanager-does-not-exist-in-the-current-context) – Matt Dec 20 '16 at 09:25

4 Answers4

18

The ConfigurationManager class in System.Configuration:

ConfigurationManager.AppSettings

ConfigurationManager.ConnectionStrings

So your code would change to:

var values = new NameValueCollection 
{ 
    { "key", ConfigurationManager.AppSettings["API-Key"] }, 
    { "image", Convert.ToBase64String(File.ReadAllBytes(photo.ToString())) } 
}; 

Make sure you add a reference to System.Configuration along with the using statement for System.Configuration.

mason
  • 31,774
  • 10
  • 77
  • 121
Kelsey
  • 47,246
  • 16
  • 124
  • 162
  • If you can't see ConfigurationManager then please refer to this link: http://stackoverflow.com/questions/1274852/the-name-configurationmanager-does-not-exist-in-the-current-context – John M Jul 04 '16 at 14:18
7

Use the System.Configuration.ConfigurationManager class

string ServerName = System.Configuration.ConfigurationManager.AppSettings["Servername"];

Edit - added

Note, you may have to add a reference to System.Configuration.dll. Even if you can import the namespace without the reference, you will not be able to access this class unless you have the reference.

mason
  • 31,774
  • 10
  • 77
  • 121
David
  • 72,686
  • 18
  • 132
  • 173
  • That's really strange, I System.Configuration namespace imported, but the ConfigurationManager class still shows up red. It's like it's not a part of the namespace. Any suggestions? I tried right-clicking and resolve, but no dice. – Sergio Tapia Aug 10 '10 at 16:27
  • 1
    See my added note. That threw me the first time as well. I think it took me a half an hour to figure it out. – David Aug 10 '10 at 16:28
3

With the ConfigurationManager class

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

Eton B.
  • 6,121
  • 5
  • 31
  • 43
2

The new class to use is the ConfigurationManager class.

Adam V
  • 6,256
  • 3
  • 40
  • 52