How do I import application settings from one VB.NET project to another?

Marc Menzel 61 Reputation points
2022-04-26T22:00:10.103+00:00

I have several VB.NET projects that use the same application settings. They use the My.Settings object. I am not sure how to import application settings from one VB.NET project to another. What isa the best way to do that?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,569 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 26,426 Reputation points Microsoft Vendor
    2022-04-27T05:34:05.603+00:00

    Hi @Marc Menzel ,
    Maybe you can use the code below to export/import settings.

        Private Sub export_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles export.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 Configuration.SettingsPropertyValue In My.Settings.PropertyValues  
      
                        sWriter.WriteLine(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  
      
        Private Sub import_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles import.Click  
      
            Dim oDialog As New OpenFileDialog  
            oDialog.Filter = "Application Settings (*.AppSettings)|*AppSettings"  
      
            If oDialog.ShowDialog() = DialogResult.OK Then  
      
                Using sReader As New StreamReader(oDialog.FileName)  
      
                    While sReader.Peek() > 0  
      
                        Dim input = sReader.ReadLine()  
      
                        ' Split comma delimited data ( SettingName,SettingValue )    
                        Dim dataSplit = input.Split(CChar(","))  
      
                        '           Setting         Value    
                        My.Settings(dataSplit(0)) = dataSplit(1)  
      
                    End While  
      
                End Using  
      
                My.Settings.Save()  
      
                MessageBox.Show("Import of settings successfull", "Import", MessageBoxButtons.OK, MessageBoxIcon.Information)  
      
            End If  
        End Sub  
    

    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Marc Menzel 61 Reputation points
    2022-04-30T00:15:54+00:00

    I will try it