C# ConfigurationSettings.AppSettings 已过时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15589129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ConfigurationSettings.AppSettings is obsolete
提问by Daniel Kmak
The following code works fine:
以下代码工作正常:
string api_url = ConfigurationSettings.AppSettings["api-url"].ToString();
with a warning message as follows:
警告信息如下:
'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: '"This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings"'
'System.Configuration.ConfigurationSettings.AppSettings' 已过时:'"此方法已过时,已被 System.Configuration!System.Configuration.ConfigurationManager.AppSettings 取代"'
As suggested by the warning message, I tried replacing ConfigurationSettings.AppSettings
with ConfigurationManager.AppSettings
正如警告消息所建议的那样,我尝试替换ConfigurationSettings.AppSettings
为ConfigurationManager.AppSettings
string api_url = ConfigurationManager.AppSettings["api-url"].ToString();
Now an error message appears, stating:
现在会出现一条错误消息,指出:
The name 'ConfigurationManager' does not exist in the current context
当前上下文中不存在名称“ConfigurationManager”
These are the namespaces imported:
这些是导入的命名空间:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
Please help me.
请帮我。
采纳答案by Vortex
Not only do you need to add System.Configuration
in front of ConfigurationManager.AppSettings["api-url"].ToString();
you also have to add the reference to the assembly System.Configuration.dll
.
你不仅需要System.Configuration
在ConfigurationManager.AppSettings["api-url"].ToString();
你前面添加,还必须添加对程序集的引用System.Configuration.dll
。
Here is link to similar question The name 'ConfigurationManager' does not exist in the current context
回答by Nick Binnet
Add reference to System.Configuration.
添加对 System.Configuration 的引用。
回答by manojlds
Add a reference to System.Configuration
. The deprecated one was in System.dll
but the same namespace System.Configuration
添加对System.Configuration
. 已弃用System.dll
的名称在相同的命名空间中System.Configuration