ConfigurationSettings.AppSettings is obsolete, war

2019-02-10 12:04发布

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?

4条回答
smile是对你的礼貌
2楼-- · 2019-02-10 12:15

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.

查看更多
甜甜的少女心
4楼-- · 2019-02-10 12:22

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.

查看更多
唯我独甜
5楼-- · 2019-02-10 12:27

The new class to use is the ConfigurationManager class.

查看更多
登录 后发表回答