C# ConfigurationManager 的命名空间在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12727721/
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
Where is ConfigurationManager's namespace?
提问by
I've got a reference to System.Configuration- and ConfigurationSettingsis found no problem - but the type or namespace 'ConfigurationManager' could not be found!?
我有一个引用System.Configuration- 并ConfigurationSettings没有发现任何问题 - 但找不到类型或命名空间“ ConfigurationManager”!?
I've read around - and it looks like it should belong to the same namespace as the ConfigurationSettingsclass.
我已经阅读了 - 看起来它应该与ConfigurationSettings类属于同一个命名空间。
EDIT: Please take note - I've clearly said I have added a reference to System.Configuration. There's been 4 comments already about this.
编辑:请注意 -我已经明确说过我已经添加了对 System.Configuration 的引用。已经有 4 条评论与此相关。
采纳答案by Ashwin Chandran
ConfigurationManager is a part of the System.Configuration after .Net 2.0. Add a reference to the System.Configuration dll. Try using System.Configuration.ConfigurationManager.
ConfigurationManager 是 .Net 2.0 之后 System.Configuration 的一部分。添加对 System.Configuration dll 的引用。尝试使用 System.Configuration.ConfigurationManager。
回答by BlakeH
You need to add a reference the System.Configuration assembly. This namespace was split across a few assemblies.
您需要添加对 System.Configuration 程序集的引用。这个命名空间被分成几个程序集。
回答by S..
You need to use the System Configuration namespace, this must be included in two ways:
您需要使用 System Configuration 命名空间,这必须以两种方式包含:
Right click the project and choose add reference, browse "Assemblies" and tick the System.Configurationassembly.
Include the namespace in code:
using System.Configuration;
右键单击项目并选择添加引用,浏览“程序集”并勾选System.Configuration程序集。
在代码中包含命名空间:
使用 System.Configuration;
回答by parker
You will need to add the System.Configuration reference under the Reference folder in your project. Only adding the system.configuration with the using statement in your class is not enough.
您需要在项目的 Reference 文件夹下添加 System.Configuration 引用。仅在类中添加带有 using 语句的 system.configuration 是不够的。
回答by Marcelo Moraes
I'm working in VisualStudio 2015, and ConfigurationManageris not present at System.Configurationnamespace anymore.
我在 VisualStudio 2015 中工作,并且ConfigurationManager不再出现在System.Configuration命名空间中。
I was just trying to read the App.config file...
我只是想读取 App.config 文件...
Then I found a solution:
然后我找到了一个解决方案:
Get:
得到:
string str = MyProjectNamespace.Properties.Settings.Default["MySettingName"].ToString();
Set:
放:
MyProjectNamespace.Properties.Settings.Default["MySettingName"]="myString";
MyProjectNamespace.Properties.Settings.Default.Save();
回答by Alan
I am using VS 2017, I added 'using System.Configuration' directive and also follow the step to add a reference assembly to the project but IDE still complaint configuration manager does not exist, it turn out it is due to the .net core installed by Nuget just add a empty container. The package System.Configuration.Configurationmanager 4.5.0 need to be installed as well. Start Tools -> Nuget Package Manager -> Package Manager Console and type "Install-Package System.Configuration.ConfigurationManager -Version 4.5.0" and this resolve the problem. Refer below screenshot in Object Browser in VS2017
我正在使用 VS 2017,我添加了“使用 System.Configuration”指令,并按照步骤将引用程序集添加到项目中,但 IDE 仍然抱怨配置管理器不存在,事实证明这是由于安装了 .net 核心通过 Nuget 只需添加一个空容器。还需要安装 System.Configuration.Configurationmanager 4.5.0 包。启动 Tools -> Nuget Package Manager -> Package Manager Console 并键入“Install-Package System.Configuration.ConfigurationManager -Version 4.5.0”,这解决了问题。在 VS2017 中的对象浏览器中参考以下屏幕截图


回答by zokaee hamid
For me, this problem was solved when I put the "BuildConnectionString" function in a class and added "Imports System.Configuration" at the top of the class page. I did as the Microsoft site itself says. https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/connection-string-builders
对我来说,当我将“BuildConnectionString”函数放在一个类中并在类页面顶部添加“Imports System.Configuration”时,这个问题就解决了。我按照微软网站本身的说法做了。 https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/connection-string-builders
Private Sub BuildConnectionString(ByVal dataSource As String, _
ByVal userName As String, ByVal userPassword As String)
' Retrieve the partial connection string named databaseConnection
' from the application's app.config or web.config file.
Dim settings As ConnectionStringSettings = _
ConfigurationManager.ConnectionStrings("partialConnectString")
If Not settings Is Nothing Then
' Retrieve the partial connection string.
Dim connectString As String = settings.ConnectionString
Console.WriteLine("Original: {0}", connectString)
' Create a new SqlConnectionStringBuilder based on the
' partial connection string retrieved from the config file.
Dim builder As New SqlConnectionStringBuilder(connectString)
' Supply the additional values.
builder.DataSource = dataSource
builder.UserID = userName
builder.Password = userPassword
Console.WriteLine("Modified: {0}", builder.ConnectionString)
End If
End Sub
The important point is to use the function in the class. When I did this the error failed to find the source.
重点是在类中使用函数。当我这样做时,错误找不到来源。

