c# Appsettings 给出错误 : The name 'ConfigurationManager' does not exist in the current context

标签 c# .net configurationmanager

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

       using System.Configuration;

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

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

虽然我收到以下错误:

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

我不确定我错过了什么。

最佳答案

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

幸运的是,System.Configuration.dll 位于 .NET 基础框架中,因此您只需要通过右键单击您的 References 文件夹来添加引用项目,单击添加引用,然后在.NET 选项卡下找到System.Configuration

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

关于c# Appsettings 给出错误 : The name 'ConfigurationManager' does not exist in the current context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13370011/

相关文章:

c# - AppSettings 从 .config 文件获取值

c# - 如何在 C# 中编写事件和事件处理程序?

c# - 最接近 F# 匹配表达式的 C#?

c# - 根据特定比较器获取两个列表中的对象

c# - 构造表达式树导致InvalidPathException

.net - 使用免注册 COM 库时出现问题

salt-stack - 如果其中一个语句失败,如何使整个 state.sls 失败

c# - ASP.NET 正则表达式问题

c# - 我如何在 Nunit/Selenium GRID/C# Setup 中使用多个浏览器

c# - ConfigurationProperty 由于其保护级别而无法访问