2
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
localSettings.Values["stupidcrap"] = "test1";

and then after restarting the app in Visual Studio:

ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
Debug.WriteLine(localSettings.Values["stupidcrap"]);

it prints nothing (which means the Object is null).

Why does that happen?

When I do it like this:

ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
localSettings.Values["stupidcrap"] = "test1";
Debug.WriteLine(localSettings.Values["stupidcrap"]);

it prints successfully, but the point of storage is that the values being saved are available after a restart of the app....

Why is the memory/storage/whatever not saving my values?

Charles
  • 50,943
  • 13
  • 104
  • 142
Alexander Ciesielski
  • 10,506
  • 5
  • 45
  • 66

1 Answers1

7

When you want to add a value, you have to call localSettings.Values.Add("key", "test1")

I'm using the following class as a settings helper:

public class StorageSettingsApplication
{
    public StorageSettingsApplication()
    {
        try
        {
            localSettings = ApplicationData.Current.LocalSettings;
        }
        catch (Exception)
        {
        }
    }
    ApplicationDataContainer localSettings;
    publicTValue TryGetValueWithDefault<TValue>(string key, TValue defaultvalue)
    {
        TValue value;

        // If the key exists, retrieve the value.
        if (localSettings.Values.ContainsKey(key))
        {
            value = (TValue)localSettings.Values[key];
        }
        // Otherwise, use the default value.
        else
        {
            value = defaultvalue;
        }

        return value;
    }

    public bool AddOrUpdateValue(string key, object value)
    {
        bool valueChanged = false;

        // If the key exists
        //if (localSettings.Contains(Key))
        if (localSettings.Values.ContainsKey(key))
        {
            // If the value has changed
            if (localSettings.Values[key] != value)
            {
                // Store the new value
                localSettings.Values[key] = value;
                valueChanged = true;
            }
        }
        // Otherwise create the key.
        else
        {
            localSettings.Values.Add(key, value);
            valueChanged = true;
        }

        return valueChanged;
    }

}
Stefan Wexel
  • 1,154
  • 1
  • 8
  • 14
  • 3
    Thanks that works. What is weird is that in the tutorials from MSDN, it says to just use it through localSettings.Values["asd"] = value; – Alexander Ciesielski Jun 26 '13 at 12:24
  • 1
    I wasted hours of my life on this exact issue and left feedback on the MSDN web page to ask them to update the page example with the localSettings.Values.Add(key, value) line. – timv Mar 11 '14 at 03:28
  • In my case it still not save settings – piotrbalut Jul 08 '15 at 12:25
  • @user3149172 Are you sure? Make sure you don't force close the app when debugging, force closing will discard all changes. This code works perfectly in 6 apps of mine (both Windows 8.1 and Windows Phone) – Stefan Wexel Jul 08 '15 at 18:29
  • What this means ? I set the key and then "Suspend and shutdown" in VS. When I run the app next time there aren't any key. – piotrbalut Jul 09 '15 at 05:51
  • While I'm using Release it works great - in debug still lose key. – piotrbalut Jul 09 '15 at 06:23
  • @user3149172 Not sure how exactly it behaves when debugging. Only know that when closing the app by stopping debugging it doesn't save. – Stefan Wexel Jul 09 '15 at 10:33