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
  • 46,456
  • 16
  • 122
  • 161
Sergio Tapia
  • 38,204
  • 73
  • 178
  • 253
  • 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
  • 30,041
  • 9
  • 73
  • 113
Kelsey
  • 46,456
  • 16
  • 122
  • 161
  • 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
  • 30,041
  • 9
  • 73
  • 113
David
  • 70,778
  • 16
  • 128
  • 169
  • 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.
  • 5,813
  • 5
  • 29
  • 43
2

The new class to use is the ConfigurationManager class.

Adam V
  • 6,068
  • 3
  • 38
  • 51