.net System.Configuration.ConfigurationManager 不可用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/851783/
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
System.Configuration.ConfigurationManager not available?
提问by kpollock
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.
在 VS2005 C# 项目中,我添加了对 System.configuration 的引用。在对象浏览器中,我可以看到 System.Configuration.ConfigurationManager。在 Intellisense System.Configuration 中只有旧的 ConfigurationSettings,而不是 ConfigurationManager。
My code System.Configuration.ConfigurationManager.AppSettings["MySetting"]
我的代码 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?
在不同的项目中,完全相同的设置工作得很好......关于发生了什么的任何线索?
采纳答案by kpollock
urgh - PICNIC error. Added ref to the wrong project in the solution...
呃 - 野餐错误。在解决方案中添加了对错误项目的引用...
回答by
Although the using System.Configuration; command is automatically generated in the using section, for some reason the actual reference is not set.
虽然 使用 System.Configuration; 命令在 using 部分自动生成,由于某种原因未设置实际引用。
Go into add reference, .Net tab, and choose System.Configuration.
进入add reference, .Net 选项卡,然后选择System.Configuration。
ConfigurationManager will now be resolved.
ConfigurationManager 现在将得到解决。
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.
如果您转到完全相同设置工作正常的项目并查看参考资料,您将看到对 System.Configuration 的参考资料。
回答by dyslexicanaboko
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.
要回答这个问题(不是已经 5 次以上没有回答)是添加 System.Configuration 作为对您的项目的引用。
However what I would really like to highlight isthat 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.
然而,我真正想强调的是,在很多情况下,我已将 System.Configuration.dll 添加到我的项目引用中,但出于某种特殊原因,有时即使添加对 System.ConfigurationManager 的引用后,ConfigurationManager 仍不会显示在智能感知中。配置。即使我删除引用并再次添加它。
The very simple solution to this problem is to:
这个问题的非常简单的解决方案是:
- Add the reference to System.Configuration.dll to your project
- Save your files
- Save your Solution
- Close the instance of Visual Studio that is giving you a hard time
- Re-open your solution
- 将 System.Configuration.dll 的引用添加到您的项目中
- 保存您的文件
- 保存您的解决方案
- 关闭给您带来困难的 Visual Studio 实例
- 重新打开您的解决方案
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.
这个简单的练习将使 Visual Studio 再次运行,并不再告诉您您没有添加对 System.Configuration 的引用。这个练习通常可以帮助我解决所有无法解释的 Visual Studio 行为。
I have had this happen to me in both VS2008 and VS2010 multiple times and it works every time.
我在 VS2008 和 VS2010 中多次发生这种情况,并且每次都有效。
回答by kpollock
For anyone who switches back and forth between developing ASP.NET WebForms and WinForms, this tip may come in handy.
对于在开发 ASP.NET WebForms 和 WinForms 之间来回切换的任何人,这个技巧可能会派上用场。
If you are developing in a C# WinForms project, you will find that attempting to use a ConfigurationManagerto get at your app.configsettings will result in the following error:
如果你在 C# WinForms 项目中开发,你会发现尝试使用 aConfigurationManager来获取你的app.config设置会导致以下错误:
The name 'ConfigurationManager' does not exist in the current context
当前上下文中不存在名称“ConfigurationManager”
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.
由于这是默认包含在 ASP.NET 项目中的,这可能会让人感到意外。只需右键单击项目中的“引用”节点,然后查看“.NET”选项卡。向下滚动,您应该找到System.Configuration. 将此添加到您的项目中,您应该已启动并运行。
Adding a Reference to System.Configuration
添加对 System.Configuration 的引用
Provided you have already added System.Configurationto 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:
如果您已经添加System.Configuration到代码顶部的 using 部分,您现在应该能够使用配置设置(例如连接字符串)和如下代码:
con.ConnectionString = ConfigurationManager.ConnectionStrings[sConnection].ConnectionString;
回答by iliketocode
From the MSDN documentation -
从 MSDN 文档 -
To use the ConfigurationManager class, your project must reference the System.Configuration assembly. By default, some project templates, like Console Application, do not reference this assembly so you must manually reference it.
要使用 ConfigurationManager 类,您的项目必须引用 System.Configuration 程序集。默认情况下,某些项目模板(如控制台应用程序)不引用此程序集,因此您必须手动引用它。
https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx
https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx
回答by Maneesh Chaudhary
回答by TheVillageIdiot
I think you need to implicitly refer to System.Configurationassembly.
我认为您需要隐式引用System.Configuration程序集。
回答by TheVillageIdiot
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
这只是一个猜测,但也许您应该检查您的项目是否至少使用 .NET framework 2.0。从 .NET 2.0 开始,ConfigurationManager 类可用,如下所示: msdn 上的链接


