1

I'm writing a Windows Forms application in Visual Studio, and I have a user-scope application setting of type string that has a default value, which is specified at design-time using the Project Designer's "Settings" page.

In my application, I have a dialog where the user can change the setting to a custom value. The dialog also has a checkbox labeled "Use Default Value". If that box is checked, I want the setting to revert to the application default.

I managed to do this by "statically" by setting it to the current default value like so: My.Settings.MyStringSetting = My.Settings.Properties.Item("MyStringSetting").DefaultValue. However, this isn't ideal because it's not persistent. If the application default changes due to an update, the user value will still be set to the old default value.

I would like to know if there is a way to simply remove the user setting override from the user.config file altogether, as My.Settings.Reset does, but for this one single setting. So that checking "Use Default Setting" will hold true even if the application default gets updated.

Is this possible?

Sigenes
  • 445
  • 2
  • 13

1 Answers1

0

I'm not sure if there's a way to remove one settings-property altogether from the user.config file. However, one way to get around this problem:

If the application default changes due to an update, the user value will still be set to the old default value.

..is to do something like this:

  • Add a settings-property called UpgradeRequired1 and set its default value to True.
  • Add another property to reflect the Checked status of the "Use Default Value" CheckBox (if you don't have it already). For this demo, I'll call it UseMyStringSettingDefaultValue.
  • When you have an update for the software, UpgradeRequired will be True by default (assuming that the assembly version is changed). So, execute the following code when your application starts:

    If My.Settings.UpgradeRequired Then
        My.Settings.Upgrade()
        My.Settings.UpgradeRequired = False
        If My.Settings.UseMyStringSettingDefaultValue Then
            My.Settings.MyStringSetting = ' The new default value ↴
                My.Settings.Properties.Item("MyStringSetting").DefaultValue.ToString()
        End If
        My.Settings.Save()
    End If
    

1 You are probably going to need it anyway to handle upgrading the settings correctly.