vb.netclickoncemy.settingsuser.configautocreate

Autocreate user.config file in deployed app


I'm coding a VB.Net AddIn for outlook (hence its VBA with .NET). It's an addition to the standard Calender Item screen. Everything's going fine. But i have a third form with two textboxes, that show me the filepath of the running assembly and the user.config path with two buttons "Open in Explorer...".

BUT: The user.config file is not there in the beginning. The parent directory and itself get created when I change a setting in the second form and save it for the first time.

I tried to save the usersettings on the form_Initialization method of my main form, with My.Settings.save(), but it won't generate the user.config file which is odd. That should be the same what I'm doing on the second form.

Can you help me? I need the user.config file right at the start of the third form.

My Forms (Image)

Code which generates the user.config file:

Private Sub BtnSaveSettings_Click(sender As Object, e As EventArgs)
        If (CmbxDebugOption.Text <> "") And (CmbxOnBootActive.Text <> "") Then

            ' save active start option
            If CmbxOnBootActive.SelectedItem.ToString.ToLower = "ja" Then
                My.Settings.ActiveOnStart = True
            ElseIf CmbxOnBootActive.SelectedItem.ToString.ToLower = "nein" Then
                My.Settings.ActiveOnStart = False
            End If

            ' save debug option
            If CmbxDebugOption.SelectedItem.ToString.ToLower = "deaktiviert" Then
                My.Settings.AlwaysDebug = False
                My.Settings.OnetimeDebug = False
            ElseIf CmbxDebugOption.SelectedItem.ToString.ToLower = "nur einmal" Then
                My.Settings.AlwaysDebug = False
                My.Settings.OnetimeDebug = True
            ElseIf CmbxDebugOption.SelectedItem.ToString.ToLower = "immer aktiv" Then
                My.Settings.AlwaysDebug = True
                My.Settings.OnetimeDebug = False
            End If
            My.Settings.Save()
        End If

        ' Close Form
        Me.Close()
    End Sub

Solution

  • I found a solution for myself. I created a variable in My.Settings called FirstRunAfterInstall, which I set to False on the first Run of my mainform. I also set the other variables to some default and then save the whole mess. The file will then get written onto your harddisk.

    Looks like this:

        If My.Settings.FirstRunAfterInstall Then
            My.Settings.FirstRunAfterInstall = False
            My.Settings.ActiveOnStart = False
            My.Settings.OnetimeDebug = False
            My.Settings.AlwaysDebug = False
            My.Settings.Save()
        End If
    

    My.Settings.Save() is the most important part! It flushes some cache. If you forget this part the changes wont get written into the file.

    Greetings from Germany,

    Christian Hase