C# 当前上下文中不存在名称“ConfigurationManager”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1274852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The name 'ConfigurationManager' does not exist in the current context
提问by pencilslate
I am trying to access connectionStrings
from the config file. The code is ASP.NET + C#. I have added System.Configuration
to reference and also mentioned with using. But still it wouldn't accept the assembly.
我正在尝试connectionStrings
从配置文件访问。代码是 ASP.NET + C#。我已经添加System.Configuration
到参考中,也提到了使用。但它仍然不会接受程序集。
I am using VSTS 2008. Any idea what could be the reason?
我正在使用 VSTS 2008。知道是什么原因吗?
Another weird thing is the assembly name shown as "System.configuration", a lower case c which is not how names are displayed for other System assemblies.
另一个奇怪的事情是程序集名称显示为“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; }
}
}
}
Config:
配置:
<?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="Data Source=localhost;Initial Catalog=qbank;Persist Security Info=True;User ID=**;Password=****;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
采纳答案by pencilslate
回答by Dan Diplo
Are you sure you have added a reference to the .NET assembly and not something else? I'd remove your reference and then try re-adding it, making sure you select from the .NET tab in Visual Studio reference dialogue - the latest version should be 2.0.0.0 in GAC.
您确定添加了对 .NET 程序集的引用而不是其他内容吗?我会删除您的参考,然后尝试重新添加它,确保您从 Visual Studio 参考对话框中的 .NET 选项卡中进行选择 - GAC 中的最新版本应该是 2.0.0.0。
回答by Nevada
For a sanity check, try creating a new Web Application Project, open the code behind for the Default.aspx page. Add a line in Page_Load to access your connection string.
为了检查完整性,请尝试创建一个新的 Web 应用程序项目,打开 Default.aspx 页面的隐藏代码。在 Page_Load 中添加一行以访问您的连接字符串。
It should have System.Configuration added as reference by default. You should also see the using statement at the top of your code file already.
默认情况下,它应该添加 System.Configuration 作为参考。您还应该已经在代码文件的顶部看到 using 语句。
My code behind file now looks like this and compiles with no problems.
我的文件隐藏代码现在看起来像这样并且编译没有问题。
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connString = ConfigurationManager.ConnectionStrings["MyConnectionStringName"].ConnectionString;
}
}
}
This assumes I have a connection string in my web.config with a name equal to "MyConnectionStringName" like so...
这假设我的 web.config 中有一个连接字符串,其名称等于“MyConnectionStringName”,就像这样......
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="MyConnectionStringName"
connectionString="Data Source=.;Initial Catalog=MyDatabase;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Yeah, it's elementary I know. But if you don't have any better ideas sometimes it helps to check against something really simple that you know should work.
是的,我知道这是初级的。但是,如果您没有更好的想法,有时检查一些您知道应该工作的非常简单的东西会有所帮助。
回答by Kieran
It's not only necessary to use the namespaceSystem.Configuration
. You have also to add the reference to the assemblySystem.Configuration.dll
, by
不仅需要使用namespaceSystem.Configuration
。你也将引用添加到组装System.Configuration.dll
,由
- Right-click on the References / Dependencies
- Choose Add Reference
- Find and add
System.Configuration
.
- 右键单击引用/依赖项
- 选择添加参考
- 查找并添加
System.Configuration
.
This will work for sure.
Also for the NameValueCollection
you have to write:
这肯定会起作用。也为NameValueCollection
你必须写:
using System.Collections.Specialized;
回答by Emu
I have gotten a better solution for the issue configurationmanager does not exist in the current context
.
我已经为这个问题找到了更好的解决方案configurationmanager does not exist in the current context
。
To a read connection string from web.config
we need to use ConfigurationManager
class and its method. If you want to use you need to add namespace using System.Configuration
;
要从中读取连接字符串,web.config
我们需要使用ConfigurationManager
类及其方法。如果要使用,则需要使用添加命名空间System.Configuration
;
Though you used this namespace, when you try to use the ConfigurationManager
class then the system shows an error “configurationmanager does not exist in the current context”.
To solve this Problem:
尽管您使用了这个命名空间,但是当您尝试使用ConfigurationManager
该类时,系统会显示错误“当前上下文中不存在配置管理器”。要解决此问题:
ConfigurationManager.ConnectionStrings["ConnectionSql"].ConnectionString;
回答by Steve Rey
If this code is on a separate project, like a library project. Don't forgeet to add reference to system.configuration.
如果此代码位于单独的项目中,例如库项目。不要忘记添加对 system.configuration 的引用。
回答by Ike
You may also get this error if you add a reference to a different, unrelated project by mistake. Check if that applies to you.
如果您错误地添加了对不同的、不相关的项目的引用,您也可能会收到此错误。检查这是否适用于您。
回答by Rameez
In your project, right-click, Add Reference..., in the .NET tab, find the System.Configuration
component name and click OK.
在您的项目中,右键单击Add Reference...,在 .NET 选项卡中,找到System.Configuration
组件名称并单击 OK。
using System.Configuration
tells the compiler/IntelliSense to search in that namespace for any classes you use. Otherwise, you would have to use the full name (System.Configuration.ConfigurationManager
) every time. But if you don't add the reference, that namespace/class will not be found anywhere.
using System.Configuration
告诉编译器/智能感知在该命名空间中搜索您使用的任何类。否则,您System.Configuration.ConfigurationManager
每次都必须使用全名 ( )。但是,如果您不添加引用,则在任何地方都找不到该命名空间/类。
Note that a DLL can have any namespace, so the file System.Configuration.dll
could, in theory, have the namespace Some.Random.Name
. For clarity/consistency they're usually the same, but there are exceptions.
请注意,DLL 可以具有任何命名空间,因此System.Configuration.dll
理论上该文件可以具有 namespace Some.Random.Name
。为了清晰/一致性,它们通常是相同的,但也有例外。
回答by Matt
If you're getting a lot of warnings (in my case 64 in a solution!) like
如果你收到很多警告(在我的例子中是 64 个解决方案!)
CS0618: 'ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'
CS0618:“ConfigurationSettings.AppSettings”已过时:“此方法已过时,已被 System.Configuration!System.Configuration.ConfigurationManager.AppSettings 取代”
because you're upgrading an older project you can save a lot of time as follows:
因为您正在升级旧项目,所以可以节省大量时间,如下所示:
- Add
System.Configuration
as a reference to your Referencessection. Add the following two
using
statements to the top of each class (.cs
) file:using System.Configuration; using ConfigurationSettings = System.Configuration.ConfigurationManager;
- 添加
System.Configuration
作为对您的参考部分的参考。 在
using
每个类 (.cs
) 文件的顶部添加以下两条语句:using System.Configuration; using ConfigurationSettings = System.Configuration.ConfigurationManager;
By this change all occurances of
通过这种变化,所有发生的
ConfigurationSettings.AppSettings["mySetting"]
will now reference the right configuration manager, no longer the deprecated one, and all the CS0618 warnings will go away immediately.
现在将引用正确的配置管理器,不再是已弃用的配置管理器,所有 CS0618 警告将立即消失。
Of course, keep in mind that this is a quick hack. On the long term, you should consider refactoring the code.
当然,请记住,这是一个快速的技巧。从长远来看,您应该考虑重构代码。
回答by Keshav Patel
To Solve this problem go to Solution Explorer And right click reference and click add reference and chose .net and find system.configuration select an click ok
要解决此问题,请转到解决方案资源管理器并右键单击引用并单击添加引用并选择 .net 并找到 system.configuration 选择单击确定