Jeremy
Jeremy

Reputation: 3951

Iterate through connections strings in App.Config file in VB.Net

I am trying to iterate through all the connection strings in App.Config using VB.net.

I would like to: 1. Get a count of all the connection strings 2. Put them all into a listbox.

I have tried using System.Configuration.ConfigurationSettings but am unsure exactly how to get the collection/listof connection strings.

The application is a WinForms VB.net .net 4.0 app.

Upvotes: 4

Views: 5750

Answers (1)

steinar
steinar

Reputation: 9663

This should work:

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

Note: If you haven't got a statement in your app.config, you'll probably also get LocalSqlServer, as that's by default defined in Machine.config.

Upvotes: 6

Related Questions