C# 错误:当前上下文中不存在名称“ConfigurationManager”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1751087/
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
Error: The name 'ConfigurationManager' does not exist in the current context
提问by zack
I have included the following statement in my Visual C# Console Application (Visual Studio 2005 .NET 2.0 Framework)
我在我的 Visual C# 控制台应用程序(Visual Studio 2005 .NET 2.0 Framework)中包含了以下语句
using System.Configuration;
and I am using the following statement in my application:
我在我的应用程序中使用以下语句:
ConfigurationManager.AppSettings["SomeStringOverHere"];
I try to build the application and I get the error: The name 'ConfigurationManager' does not exist in the current context.
我尝试构建应用程序,但出现错误:当前上下文中不存在名称“ConfigurationManager”。
Any help please?
请问有什么帮助吗?
采纳答案by Philip Rieck
You need to reference System.Configuration.dll in your project as well as the "using" statement.
您需要在项目中引用 System.Configuration.dll 以及“using”语句。
Namespaces are (sometimes) "split" across assemblies. That means that types in a single namespace are actually in different assemblies.
命名空间(有时)跨程序集“拆分”。这意味着单个命名空间中的类型实际上位于不同的程序集中。
To determine which assembly a BCL or FCL type is in, look it up on MSDN. If you look at the help for ConfigurationManager, you'll see that it specifies that it's in the System.Configuration assembly by looking near the top at "Assembly". This is the assembly you need to reference from your project
要确定 BCL 或 FCL 类型所在的程序集,请在 MSDN 上查找。如果您查看ConfigurationManager的帮助,您会看到它通过查看“程序集”的顶部附近来指定它在 System.Configuration 程序集中。这是您需要从项目中引用的程序集
回答by Taylor Brown
Philip was correct adding the reference helped me, but I actually went and tried to download the DLL because I did not know there was an easier way...
菲利普是正确的,添加参考对我有帮助,但我实际上去尝试下载 DLL,因为我不知道有更简单的方法......
- right-click on 'add references' folder in the solution explorer
- select the '.NET' tab
- search for the .NET reference you would like to add (in this case System.Configuration)
- 右键单击解决方案资源管理器中的“添加引用”文件夹
- 选择“.NET”选项卡
- 搜索您要添加的 .NET 引用(在本例中为 System.Configuration)
This post was very helpful to me, thanks to all.
这篇文章对我很有帮助,谢谢大家。
回答by ?????
I faced the same problem too.
我也面临同样的问题。
I have 2 projects 1 main and 1 DLL. I have App.config file in the same Place But Connection string was defined in DLL but not in main project which is executable one as main. after adding / moving the connection string to main project APP.CONFIG file, the issue resolved.
我有 2 个项目,1 个主要和 1 个 DLL。我在同一个地方有 App.config 文件,但是连接字符串是在 DLL 中定义的,而不是在主项目中定义的,主项目是可执行的。将连接字符串添加/移动到主项目 APP.CONFIG 文件后,问题解决。
Hope this helps
希望这可以帮助