在我的程序顶部,我有以下内容:
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/