0

I use Local setting to store username and password of application user.

Purpose: When user click on Remember me check, it store in local. When user restart application, it get from local setting and show.

MyApplication.Properties.Settings.Default.UserName = "john"; //get from login form MyApplication.Properties.Settings.Default.Password = "jonh@123"; //get from login form

This is working fine if application run continuously. But If I install upgrade version of the application, it behaves incorrectly. This MyApplication.Properties.Settings.Default.UserName returns older user name.

If I again install another upgrade version, it returns any other user name that I have used before.

Can anybody suggest what I am missing here?

Nanji Mange
  • 2,155
  • 4
  • 29
  • 63
  • are you calling `MyApplication.Properties.Settings.Default.Save()`? – MikeT May 10 '16 at 11:22
  • Yes he does. The problem arises whenever a new version of an application is deployed. In that case a new folder is used in the user's profile to store the settings. One must explicitly call `Settings.Default.Upgrade` do copy the previous values into the new file. – Thorsten Dittmar May 10 '16 at 11:33
  • @ThorstenDittmar I can call Upgrade setting as you suggest but I don't know How can I come to know that my application is upgraded? Can you please suggest? – Nanji Mange May 10 '16 at 11:37
  • @NanjiMange Read Barry's answer. This is the correct solution. – Thorsten Dittmar May 10 '16 at 12:05
  • @ThorstenDittmar I have post my code with suggested way. Please review: http://stackoverflow.com/questions/37139383/how-to-keep-old-local-setting-data-while-upgrading-windows-application – Nanji Mange May 10 '16 at 13:20

1 Answers1

2

Try this. Create a setting called UserSettingsUpgradeRequired, set it's value to true, and check it at startup. If it's true then this is the first run of the new version so call Settings.Default.Upgrade();

private static void CheckUserSettingsUpgradeRequired()
{
    if (Settings.Default.UserSettingsUpgradeRequired)
    {
        Settings.Default.Upgrade();
        Settings.Default.UserSettingsUpgradeRequired = false;
        Settings.Default.Save();
    }
}
Barry O'Kane
  • 1,189
  • 7
  • 12
  • 1
    @ThorstenDittmar Update. Good catch. – Barry O'Kane May 10 '16 at 11:36
  • To add to this answer: Setting this setting to `true` in the settings designer causes it to be `true` whenever (but only if) a new version of the application is installed, as only then the default settings are restored. So if the setting is found to be `true` you know you need to do an upgrade. And you will only do the upgrade once as you are then setting the value to `false` and store the settings. That way you will perform an upgrade once if you need to. – Thorsten Dittmar May 10 '16 at 12:07
  • @ThorstenDittmar `Settings.Default.Upgrade();` removes my existing data. Ex. in version 1.0.0, `UserName = "john"` and `Password = "jonh@123"`. If I install new version 1.1.0, it set both details to blank. So this is the value after upgrade `UserName = ""` and `Password = ""`. It should keep old details of `UserName` and `Password`. I don't want user enter their details again after upgrade because it has set "Remember me". Please suggest If I am missing anything. – Nanji Mange May 10 '16 at 12:24
  • @ThorstenDittmar I just want that whatever data was stored in Version 1.0.0 should be as it is after installing Version 1.1.0. It should not change. – Nanji Mange May 10 '16 at 12:28
  • Can you confirm that `Settings.Default.Upgrade();` is actually being run? Did you create the `UserSettingsUpgradeRequired` setting> – Barry O'Kane May 10 '16 at 12:31
  • @BarryO'Kane I have tried. Currently I am testing it. I have set alert on its call. – Nanji Mange May 10 '16 at 12:34
  • @BarryO'Kane To test it. Create a form. Set any value to Setting on Load. Create a button and set click event which Upgrade your setting. You will see that it remove the Setting value that you have set on Load event. – Nanji Mange May 10 '16 at 12:38
  • @BarryO'Kane As it is long code, I have post a new question: http://stackoverflow.com/questions/37139383/how-to-keep-old-local-setting-data-while-upgrading-windows-application. Please review my code and let me know if I am wrong. UserName and Password is of Type string in LocalSettings. – Nanji Mange May 10 '16 at 13:04
  • @BarryO'Kane I apologies. I misunderstood your approach. I read whole thread again in this morning. I got what you wanted to say. I applied and it is working perfectly as I required. A small thing waste wholeday. Thank you. – Nanji Mange May 11 '16 at 04:49
  • @BarryO'Kane I am stuck again. `UserSettingsUpgradeRequired` is `true` for `Version 1.0.0` but it keeps `false` when I install `Version 1.1.0` or any higher. So, it doesn't go inside the If condition. Can you suggest me? – Nanji Mange May 11 '16 at 07:17
  • @NanjiMange The situation should play out as follows. In Visual Studio `Settings.Default.UserSettingsUpgradeRequired` should be set to `true`. When the new version is run for the first time, it should be using the default values, and invoke `Settings.Default.Upgrade();`. I'm currently using the exact code above, and it's working fine for me. – Barry O'Kane May 11 '16 at 08:53