System.Configuration.ConfigurationManager 不可用?
在 VS2005 C# 项目中,我添加了对 System.configuration 的引用。 在对象浏览器中,我可以看到 System.Configuration.ConfigurationManager。 在 Intellisense System.Configuration 中只有旧的 ConfigurationSettings,而不是 ConfigurationManager。
我的代码 System.Configuration.ConfigurationManager.AppSettings["MySetting"]
突出显示为语法错误并且无法编译。
在不同的项目中,完全相同的设置工作得很好......关于发生了什么的任何线索?
In a VS2005 C# project I have added a reference to System.configuration.
In the object browser, I can see the System.Configuration.ConfigurationManager.
In Intellisense System.Configuration only has the old ConfigurationSettings, and not ConfigurationManager.
My code
System.Configuration.ConfigurationManager.AppSettings["MySetting"]
is highlighted as a syntax error and does not compile.
In a different project, the exact same setup works just fine... any clues as to what is going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
尽管使用 System.Configuration; 命令是在使用部分自动生成的,由于某种原因未设置实际参考。
进入添加引用、.Net 选项卡,然后选择System.Configuration。
ConfigurationManager 现在将得到解决。
如果您转到完全相同的设置运行良好的项目并查看引用,您将看到对 System.Configuration 的引用。
Although the using System.Configuration; command is automatically generated in the using section, for some reason the actual reference is not set.
Go into add reference, .Net tab, and choose System.Configuration.
ConfigurationManager will now be resolved.
If you go to the project where the exact same setup works just fine and look at the references, you will see a reference to System.Configuration.
要回答这个问题(并不是说它已经被回答了 5 次以上),就是添加 System.Configuration 作为对您的项目的引用。
但是我真正想强调的是,在很多情况下,我已将 System.Configuration.dll 添加到我的项目引用中,但出于某种特殊原因,有时 ConfigurationManager 仍然不会显示在智能感知中添加对 System.Configuration 的引用后。 即使我删除引用并再次添加它。
此问题的非常简单的解决方案是:
这个简单的练习将让 Visual Studio 再次正常运行并停止告诉您没有添加对 System.Configuration 的引用。 这个练习通常可以帮助我解决所有令人费解的 Visual Studio 行为。
我在 VS2008 和 VS2010 中都多次遇到过这种情况,每次都有效。
To answer the question (not that it hasn't been answered already 5+ times) is to add System.Configuration as a reference to your project.
However what I would really like to highlight is that on many occasion I have added the System.Configuration.dll to my project's references, but for whatever special reason sometimes ConfigurationManager still won't show up in intellisense even after adding the reference to System.Configuration. Even if I remove the reference and add it again.
The very simple solution to this problem is to:
This simple exercise will get Visual Studio to behave again and stop telling you that you did not add your reference to System.Configuration. This exercise usually helps me with all inexplicable Visual Studio behaviors.
I have had this happen to me in both VS2008 and VS2010 multiple times and it works every time.
呃 - 野餐错误。 在解决方案中添加了对错误项目的引用...
urgh - PICNIC error. Added ref to the wrong project in the solution...
对于在开发 ASP.NET WebForms 和 WinForms 之间来回切换的任何人来说,此技巧可能会派上用场。
如果您在 C# WinForms 项目中进行开发,您会发现尝试使用
ConfigurationManager
获取app.config
设置将导致以下错误:因为默认情况下该名称包含在 ASP.NET 项目中,因此这可能会让人感到意外。 只需右键单击项目中的“引用”节点,然后查看“.NET”选项卡即可。 向下滚动,您应该会找到
System.Configuration
。 将其添加到您的项目中,您应该可以启动并运行。添加对 System.Configuration 的引用
如果您已经将
System.Configuration
添加到代码顶部的 using 部分,那么您现在应该能够在代码中使用配置设置(例如连接字符串)例如以下内容:For anyone who switches back and forth between developing ASP.NET WebForms and WinForms, this tip may come in handy.
If you are developing in a C# WinForms project, you will find that attempting to use a
ConfigurationManager
to get at yourapp.config
settings will result in the following error:Since this is included by default in ASP.NET projects, this may come as a surprise. Just simply right-click on the "References" node in your project and look on the ".NET" tab. Scroll down and you should find
System.Configuration
. Add this to your project and you should be up and running.Adding a Reference to System.Configuration
Provided you have already added
System.Configuration
to the using section at the top of your code, you should now be able to use config settings (such as connection strings) with code such as the following:来自 MSDN 文档 -
https://msdn.microsoft.com/en-us/library /system.configuration.configurationmanager.aspx
From the MSDN documentation -
https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx
我认为您需要隐式引用 System.Configuration 程序集。
I think you need to implicitly refer to System.Configuration assembly.
这只是一个猜测,但也许您应该检查您的项目是否至少使用 .NET Framework 2.0。 ConfigurationManager 类自 .NET 2.0 起可用,如下所述:
msdn 上的链接
It's just a guess, but maybe you should check if your project is using at least .NET framework 2.0. ConfigurationManager class is availvable since .NET 2.0 as dfescribed here:
link on msdn
哇——这对我来说是一个很难解决的问题; 最后,我发现我的 vb.net 程序只有在包含引用(如预期)时才会编译,并从我的模块中删除了“Imports System.Net”语句(如我所料)。
wow - this was a hard one for me to resolve; in the end, I found that my vb.net program would only compile if included the reference (as expected) and removed the "Imports System.Net" statement (as I did not expect) from my module.