views:

764

answers:

1

I am using the built-in My.Settings functionality in VB.NET to save application settings.

This is very convenient but I notice that each time I release a new version, the settings are lost.

Why and how can I prevent it?

+2  A: 

You need to manually update your application settings, I use this easy method:

  • Create a boolean setting called MustUpgrade, User scope, default to True.

Then write a method to check if My.Settings requires updating, and call it's Update() method if so. Flag your settings as updated, and save. Call this somewhere in your app load. The Upgrade() method will update your Settings to the new format, and migrate your existing values over.

Sub UpgradeMySetings()
 If My.Settings.MustUpgrade Then
  My.Settings.Upgrade()
  My.Settings.MustUpgrade = False
  My.Settings.Save()
 End If
End Sub
Wez
Interesting...How do you set a default value for a Property Setting? The Settings tab of the App Properties dialog doesn't seem to support it. I am wondering if it would be harmful to u[grade unconditionally...
Velika
In your project properties, the Settings tab shows a grid where you can define your application settings. Last column (value) is the default value. Enter "True" into this cell. Same for 2005 and 2008, more info at http://msdn.microsoft.com/en-us/library/25zf0ze8.aspx
Wez
Ah, of course. Dummy/blind me.
Velika
Unconditional upgrades will work, the test is just good practice, and for those cases where you need to pre-process old settings before migrating them to the new ones. So the logic is there *in case* a future version needs it ;)
Wez
I take it back. It didn't work. I have the same issue as posted here:http://bytes.com/topic/visual-basic-net/answers/854235-my-settings-upgrade-doesnt-upgradeI am using click-Once to deploy the Winfform app. I suspect that each upgrade goes to a new folder. I am signing the app with a temporary signature file. This did not solve my issue as it did for the poster.
Velika
Hmm. My Deployment program is signed, but the actual app is not. I can't sign the app because not all of the binary components I used are signed.
Velika
Remember that each instance (bin\debug, bin\release, clickonce install) keeps it's own copy of settings. Also when you recreate your signature, the app is considered a 'different app', and settings won't migrate. Once you sign, keep that key (note they expire each year.)
Wez
That's why I left ClickOnce, and many other issues it gave. I use a self extractor, and the upgrade function always works great in our production apps.
Wez