"ConfigurationManager"名称在当前上下文中不存在

pen*_*ate 444 .net c# visual-studio-2008 visual-studio

我试图connectionStrings从配置文件中访问.代码是ASP.NET + C#.我已添加System.Configuration参考,并提到使用.但它仍然不接受集会.

我正在使用VSTS 2008.任何想法可能是什么原因?

另一个奇怪的事情是显示为"System.configuration"的程序集名称,一个小写的c,而不是其他系统程序集的名称显示方式.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Utility
{
    public class CommonVariables
    {
        public static String ConnectionString
        {
            get { return ConfigurationManager.ConnectionStrings["EmployeeEntities"].ConnectionString; }
        }  
    }  
}
Run Code Online (Sandbox Code Playgroud)

配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="qbankEntities" connectionString="metadata=res://*/qbankModel.csdl|res://*/qbankModel.ssdl|res://*/qbankModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=localhost;Initial Catalog=qbank;Persist Security Info=True;User ID=**;Password=****;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

Kie*_*ran 794

不仅需要使用命名空间 System.Configuration.你也将引用添加到组装 System.Configuration.dll,由

  1. 右键单击References/Dependencies
  2. 选择添加参考
  3. 查找并添加System.Configuration.

这肯定会起作用.还有NameValueCollection你必须写:

using System.Collections.Specialized;
Run Code Online (Sandbox Code Playgroud)

  • 这是我的理解:这可能是错的.添加引用时,您要求将dll复制到compile/build上的bin文件夹中.创建项目时会添加一些被视为核心的dll,即当您执行file-> new project时,将在此处设置引用.他们都必须经历相同的过程,只是有些是为你完成的,有些你必须手动完成.您可以通过删除对System dll的引用并观察所有代码失败来测试它.=) (11认同)
  • 好.所以我做了一些研究,发现上面的情况大多都是真的.但是,有些文件不需要在运行时写入bin文件夹,因为它们位于Global Assembly cashe(GAC)中,其中bin是本地程序集cashe.像System.dll一样. (6认同)
  • 'System.Configuration'命名空间可以在'System'dll中找到,这就是为什么当Intellisense接受'using System.Configuration;'时可能会有一些头疼的问题.但除非项目引用System.Configuration.dll,否则无法找到ConfigurationManager. (4认同)
  • +1 谢谢基兰。当大多数其他程序集可以通过包含 'using' 语句简单地调用时,您知道为什么必须这样做吗? (2认同)

小智 95

在项目中,右键单击" 添加引用...",在".NET"选项卡中,找到System.Configuration组件名称,然后单击"确定".

using System.Configuration告诉编译器/ IntelliSense在该命名空间中搜索您使用的任何类.否则,您System.Configuration.ConfigurationManager每次都必须使用全名().但是,如果您不添加引用,则无法在任何位置找到该命名空间/类.

请注意,DLL可以具有任何名称空间,因此System.Configuration.dll理论上该文件可以具有名称空间Some.Random.Name.为了清晰/一致,它们通常是相同的,但也有例外.


pen*_*ate 38

好的......重新启动VSTS后就可以了.该链接提出了解决同一问题的方法.希望我以前见过它.:)

  • 超过7年后,这仍然是一个耻辱,这通常是答案. (14认同)
  • 没有什么比将自己的回复标记为答案,当它确实不是答案时.太糟糕的mods无法编辑它以获得实际的正确答案.Dude标记了他自己的答案...... (4认同)

Est*_*ati 19

添加此答案,因为没有一个建议的解决方案适合我.

  1. 右键单击引用选项卡以添加引用.
  2. 单击"装配"选项卡
  3. 搜索'System.Configuration'
  4. 单击确定.


cal*_*ios 18

System.Configuration为所有项目添加as引用将解决此问题。

  1. 转到Project->Add Reference

  2. 在出现的框中,单击All assemblies左侧列表中的列表选项卡。

  3. 在中央列表中,滚动到System.Configuration并确保选中该框。

  4. 单击“确定”进行申请,您现在可以访问该ConfigurationManager课程。


Saa*_*rki 16

只需安装

Install-Package System.Configuration.ConfigurationManager -Version 4.5.0
Run Code Online (Sandbox Code Playgroud)

然后使用

using System.Configuration;
Run Code Online (Sandbox Code Playgroud)


小智 9

我已经为这个问题找到了更好的解决方案configurationmanager does not exist in the current context.

要读取连接字符串,web.config我们需要使用ConfigurationManager类及其方法.如果你想使用,你需要使用添加命名空间System.Configuration;

虽然您使用了此命名空间,但在尝试使用ConfigurationManager该类时,系统会显示错误"configurationmanager在当前上下文中不存在".要解决这个问题:

ConfigurationManager.ConnectionStrings["ConnectionSql"].ConnectionString; 
Run Code Online (Sandbox Code Playgroud)


Mat*_*att 9

如果您收到很多警告(在我的情况下是64个解决方案!)就像

CS0618:'ConfigurationSettings.AppSettings'已过时:'此方法已过时,已被System.Configuration取代!System.Configuration.ConfigurationManager.AppSettings'

因为您正在升级旧项目,所以可以节省大量时间,如下所示:

  1. 添加System.Configuration参考部分的参考.
  2. 将以下两个using语句添加到每个class(.cs)文件的顶部:

    using System.Configuration; using ConfigurationSettings = System.Configuration.ConfigurationManager;

通过这种改变所有的出现

ConfigurationSettings.AppSettings["mySetting"]
Run Code Online (Sandbox Code Playgroud)

现在将引用正确的配置管理器,不再是已弃用的配置管理器,并且所有CS0618警告将立即消失.

当然,请记住,这是一个快速的黑客.从长远来看,您应该考虑重构代码.


小智 8

  1. 右键单击项目
  2. 选择管理器 NuGet 包
  3. 查找 System.Configuration
  4. 选择 Microsoft 的 System.Configuration.ConfigurationManager
  5. 安装

现在你可以:

using System.Configuration;
Run Code Online (Sandbox Code Playgroud)


Dan*_*plo 6

您确定已添加对.NET程序集的引用而不是其他内容吗?我将删除您的引用,然后尝试重新添加它,确保您从Visual Studio参考对话框中的.NET选项卡中选择 - 最新版本应该是GAC中的2.0.0.0.