Search code examples
c#winformsvisual-studiosessionpersistence

Windows Forms Settings Values Don't Persist Between Sessions


I am developing a Windows Forms application which ideally will store inventory values between sessions.

In the past, I have been able to save the Properties.Settings values between sessions. However, using similar code with my current application I am unable to do so.

Here is the snippet of code where I attempt to save my values:

    foreach (KeyValuePair<string, double> ingredient in inventoryDictionary)
    {
        var ingredientUnit = unitList[ingredient.Key];
        lstInventory.Items.Add(item: string.Format(@"{0} {1} {2}", ingredient.Key, ingredient.Value, ingredientUnit));
        settings[ingredient.Key] = ingredient.Value;
        settings.Save();
    }

The values are stored during the active session, but once the form is closed the values always reset to default.

All of my settings are listed under the "User" scope, and have the "Type" of double as expected. Additionally, all the values in the settings are set to 0 by default.

Any assistance in this issue would be greatly appreciated. I've been able to save values between sessions before, I'm not sure why it isn't working at this point...

Thank you


Solution

  • Try with Properties.Settings.Default

    Example:

    Properties.Settings.Default.myColor = Color.AliceBlue;
    

    If you want to persist it between sessions

    Properties.Settings.Default.Save();
    

    See The MSDN link here