0

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
JRDumont
  • 1
  • 1
  • What happens if you add `My.Settings.PropertyValues.GetEnumerator().MoveNext()` before the loop? -- You know that you're getting a mix of Application and User Settings, the latter in their current value, not the default value – Jimi Jan 06 '23 at 20:53
  • If you tell us *why* you need to do that, perhaps someone who's done the same thing before because of that same reason would be able to help. – Andrew Morton Jan 06 '23 at 22:51
  • I am trying to export settings so another user can import them. – JRDumont Jan 07 '23 at 01:35

1 Answers1

3

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
dbasnett
  • 11,334
  • 2
  • 25
  • 33
  • That is (essentially) the same code the OP provided. – David Jan 06 '23 at 22:22
  • 1
    @David Notice that the OP used `My.Settings.PropertyValues` whereas the code in this answer uses `My.Settings.Properties`, which contains the items the OP needs, so it works. (Also, it corrects the WriteLine parameters.) – Andrew Morton Jan 08 '23 at 10:35