ConfigurationSettings.AppSettings已过时,警告

问题描述:

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

什么是使用app.config文件的新方法?ConfigurationSettings.AppSettings已过时,警告

+0

[名称'ConfigurationManager'在当前上下文中不存在]的可能重复(http://*.com/questions/1274852/the-name-configurationmanager-does-not-exist-in-the-current -context) – Matt 2016-12-20 09:25:41

ConfigurationManagerSystem.Configuration

ConfigurationManager.AppSettings 

ConfigurationManager.ConnectionStrings 

所以,你的代码将变为:

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

确保您的参考与using语句一起添加到System.ConfigurationSystem.Configuration

+0

如果你看不到ConfigurationManager,那么请参考下面的链接:http://*.com/questions/1274852/the-name-configurationmanager-does-not-exist-in-the-current-context – 2016-07-04 14:18:12

使用System.Configuration.ConfigurationManager

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

编辑 - 添加

注意,你可能需要添加到System.Configuration参考.dll文件。即使您可以导入没有引用的名称空间,除非您有参考,否则您将无法访问此类。

+0

这真的很奇怪,我导入了System.Configuration命名空间,但ConfigurationManager类仍然显示为红色。这就像它不是名称空间的一部分。有什么建议么?我试图右键点击并解决,但没有骰子。 – 2010-08-10 16:27:03

+1

查看我的补充说明。这也是第一次抛出我。我想我花了半个小时才弄明白。 – David 2010-08-10 16:28:12

要使用的新类是ConfigurationManager类。