tags:

views:

49

answers:

4
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?

+2  A: 

With the ConfigurationManager class

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

Eton B.
+1  A: 

Use the System.Configuration.ConfigurationManager class

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

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.

David Stratton
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
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 Stratton
+5  A: 

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"].ToString() }, 
    { "image", Convert.ToBase64String(File.ReadAllBytes(photo.ToString())) } 
}; 

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

Kelsey
+1  A: 

The new class to use is the ConfigurationManager class.

Adam V