【问题标题】:the name configurationmanager does not exist in the current context当前上下文中不存在名称 configurationmanager
【发布时间】:2019-11-05 01:24:44
【问题描述】:

我正在使用 Visual Studio Community for Mac(.NETCore.App(2.1.0)、MacOS 10.14.5)并且我正在尝试使用 ConfigurationManager 类 (https://docs.microsoft.com/de-de/dotnet/api/system.configuration.configurationmanager?view=netframework-4.8)。

但我收到错误消息:

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

我已经添加了

using System.Configuration

到我的代码中,并将 System.Configuration.dll(必须手动下载,因为参考列表完全为空)添加到参考中。

感谢您提前提供的帮助。

using System;
using System.Configuration;

namespace program
{
  public static class test{
     public static string val(string name){
        return ConfigurationManager.ConnectionStrings[name].ConnectionString;
     }
  }
}

【问题讨论】:

    标签: .net-core system.configuration


    【解决方案1】:

    你应该安装System.Configuration.ConfigurationManager 包。所以请尝试执行以下命令:

    Install-Package System.Configuration.ConfigurationManager
    

    更正

    由于你是在 Mac 上工作,我不知道你是否可以运行上面的命令。这绝对适用于 Visual Studio / Windows。因此,请按照here 的说明为上述包添加依赖项。

    【讨论】:

    • 不知道为什么我们需要这样做,因为我们以前从来不需要这样做,但这个修复对我有用。
    • 我为此浪费了半个小时。认为 System.Configuration 总是在那里,因此从未想过添加它。
    【解决方案2】:

    对于 Visual Studio 2019,我遇到了同样的问题,这就是我解决它的方法:

    右键单击项目并转到管理 NuGet 包。

    搜索 System.Configuraion 并安装 System.Configuration.ConfigurationManager

    现在在您的代码中,配置管理器将被识别。

    【讨论】:

      猜你喜欢
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 2014-04-22
      • 2017-06-17
      • 2015-09-11
      • 1970-01-01
      相关资源
      最近更新 更多