Where is ConfigurationManager's namespace?

后端 未结 7 1090
半阙折子戏
半阙折子戏 2021-01-01 09:43

I\'ve got a reference to System.Configuration - and ConfigurationSettings is found no problem - but the type or namespace \'ConfigurationMana

相关标签:
7条回答
  • 2021-01-01 09:52

    You need to use the System Configuration namespace, this must be included in two ways:

    1. Right click the project and choose add reference, browse "Assemblies" and tick the System.Configuration assembly.

    2. Include the namespace in code:

      using System.Configuration;

    0 讨论(0)
  • 2021-01-01 09:54

    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.

    0 讨论(0)
  • 2021-01-01 10:03

    You need to add a reference the System.Configuration assembly. This namespace was split across a few assemblies.

    0 讨论(0)
  • 2021-01-01 10:09

    I'm working in VisualStudio 2015, and ConfigurationManager is not present at System.Configuration namespace anymore.

    I was just trying to read the App.config file...

    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();
    
    0 讨论(0)
  • 2021-01-01 10:09

    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

    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.

    0 讨论(0)
  • 2021-01-01 10:18

    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.

    0 讨论(0)
提交回复
热议问题