在VB.Net中迭代App.Config文件中的连接字符串

Jer*_*emy 4 vb.net connection-string app-config winforms

我试图使用VB.net迭代App.Config中的所有连接字符串.

我想:1.获取所有连接字符串的计数2.将它们全部放入列表框中.

我已经尝试过使用System.Configuration.ConfigurationSettings,但我不确定如何获取collection/listof连接字符串.

该应用程序是一个WinForms VB.net .net 4.0应用程序.

ste*_*nar 6

这应该工作:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' You need to add a reference to System.Configuration

    ' Uncomment to get the count:
    ' Dim count As Int32
    ' count = Configuration.ConfigurationManager.ConnectionStrings.Count

    Dim current As Configuration.ConnectionStringSettings

    For Each current In Configuration.ConfigurationManager.ConnectionStrings
        ListBox1.Items.Add(current.Name)
    Next
End Sub
Run Code Online (Sandbox Code Playgroud)

注意:如果你的app.config中没有声明,你可能也会获得LocalSqlServer,因为默认情况下是在Machine.config中定义的.