【问题标题】:c# Appsettings giving error: The name 'ConfigurationManager' does not exist in the current contextc# Appsettings给出错误:当前上下文中不存在名称'ConfigurationManager'
【发布时间】:2014-12-11 14:26:25
【问题描述】:

在我的程序顶部,我有以下内容:

       using System.Configuration;

在我的代码中,我有以下内容:

int  CompanyID = Convert.ToInt32(ConfigurationManager.AppSettings["CompanyId"]
    .ToString());

我收到以下错误

The name 'ConfigurationManager' does not exist in the current context

我不确定我错过了什么。

【问题讨论】:

  • 您可能会得到googled 自己的答案。 :)

标签: c# .net configurationmanager


【解决方案1】:

要扩展一点,您需要添加对System.Configuration.dll 的引用才能使其正常工作。这有点误导,因为System.Configuration 命名空间也存在于基础System.dll 中,并且包含一些使用较少的对象,例如SettingsContext。结果,它似乎真的应该起作用,但事实并非如此。这确实令人困惑,并且是目前 .NET 框架中的那些迟钝的陷阱之一。

还好System.Configuration.dll在.NET基础框架中,所以你只需要在你的项目中右键References文件夹,点击Add Reference,然后在下面找到System.Configuration添加引用.NET 选项卡。

将其导入您的项目后,不要忘记将using System.Configuration添加到您打算在其中使用ConfigurationManager的代码文件的顶部。

【讨论】:

  • 你好,我做了你提到的事情:右键单击项目中的 References 文件夹,单击添加引用,然后在 .NET 选项卡下找到 System.Configuration。但我仍然收到同样的信息。我也做了一次构建,但同样的信息
  • @WebDev 天哪,好电话。我忘了在您使用它的任何地方的顶部包含using System.Configuration。我总是忘记这一点,因为我使用 resharper,它允许我自动添加我缺少的 using 语句。
【解决方案2】:

您需要在项目中添加对 System.Configuration 的引用。

【讨论】:

    猜你喜欢
    • 2010-12-17
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多