JRDumont
JRDumont

Reputation: 11

How do I loop through all the values in My.Settings

I am using Microsoft Visual Studio Community 2019 and I need to loop through all the settings in My.Settings. I would like to capture the name and value.

I found some code and I can't get it to work. The file is created but it is empty.

Any assistance would be greatly appreciated.

Here is the sample code I am testing with. My goal is to export the settings so another user can import them.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim sDialog As New SaveFileDialog()
    sDialog.DefaultExt = ".AppSettings"
    sDialog.Filter = "Application Settings (*.AppSettings)|*AppSettings"

    If sDialog.ShowDialog() = DialogResult.OK Then

        Using sWriter As New StreamWriter(sDialog.FileName)
            For Each setting As System.Configuration.SettingsPropertyValue In My.Settings.PropertyValues
                sWriter.WriteLine("{0}  {1}", setting.Name & "," & setting.PropertyValue.ToString())
            Next
        End Using

        My.Settings.Save()
        MessageBox.Show("Settings has been saved to the specified file", "Export", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End If

End Sub

Upvotes: 0

Views: 241

Answers (1)

dbasnett
dbasnett

Reputation: 11773

This worked,

    For Each val As System.Configuration.SettingsProperty In My.Settings.Properties
        Debug.WriteLine("{0}  {1}", val.Name, My.Settings.Item(val.Name))
    Next

Upvotes: 3

Related Questions