question

MarcMenzel-8677 avatar image
0 Votes"
MarcMenzel-8677 asked MarcMenzel-8677 answered

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

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?

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JiachenLiMFST-9349 avatar image
0 Votes"
JiachenLiMFST-9349 answered JiachenLiMFST-9349 commented

Hi @MarcMenzel-8677 ,
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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

How would I import the My.Settings file into my new VB.NET Project at run time? Is it possible to write a program using the code posted above to automate the process? How could I write a program to create a file that I could add to a new VB.NET project at run time?

0 Votes 0 ·

Hi @MarcMenzel-8677 ,
Have you tried the code above?
It can export the application's My.Settings to the .AppSettings file or import the settings from the .AppSettings file into My.Settings.

0 Votes 0 ·
MarcMenzel-8677 avatar image
0 Votes"
MarcMenzel-8677 answered

I will try it

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.