Storage on Windows 8 and Windows Phone (part 1)

As promised, here’s the recap of the “Local” storage on Windows 8 and Windows Phone part of my talk at the Windows 8 and Windows Phone camp in Zwolle, the Netherlands last week. In this first part we’ll go over the local and roaming storage options on Windows 8 and local storage capabilities on Windows Phone. Using SQLite warrants a post on its own!

IsolatedStorage on Windows Phone

Let’s start with the IsolatedStorage capability on Windows Phone. Back in October of 2010, Jeff Blankenburg wrote an excellent article that goes over IsolatedStorage on Windows Phone. As mentioned, it was introduced in Silverlight version 2, so if you’re familiar with that, you can just use it on Windows Phone. To refresh your memory, it’s the storage that’s available to your app and your app only. We have two main ways to access IsolatedStorage on Windows Phone: IsolatedStorageSettings and IsolatedStorageFile. As the name indicates, the first one is used for persisting settings in a key/value manner:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

settings["somekey"] = "myvalue";
settings["otherkey"] = true;

settings.Save();

Just make sure to call the Save method when you want to persist your changes. In my own projects I use a simple helper class to easily use IsolatedStorageSettings that I found a while back. I can’t really remember (or find) where I got these from, so if it’s yours do let me know!

public static bool ClearPersistent(string key)
{
    if (null == key)
        return false;

    var store = IsolatedStorageSettings.ApplicationSettings;
    if (store.Contains(key))
        store.Remove(key);
    store.Save();
    return true;
}

public static bool SavePersistent(string key, object value)
{
    if (null == value)
        return false;

    var store = IsolatedStorageSettings.ApplicationSettings;
    if (store.Contains(key))
        store[key] = value;
    else
        store.Add(key, value);

    store.Save();
    return true;
}

public static T LoadPersistent<T>(string key)
{
    var store = IsolatedStorageSettings.ApplicationSettings;
    if (!store.Contains(key))
        return default(T);

    return (T)store[key];
}

Using IsolatedStorageFile is slightly more complex, but still quite straightforward:

using (IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
    fileStorage.CreateDirectory("myFiles");
 
    using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(@"myFiles&#092;someFile.txt", FileMode.Create, FileAccess.Write))
    {
        using (StreamWriter writer = new StreamWriter(fileStream))
        {
            var someText = "This text wil be saved to IsolatedStorage.";
            writer.WriteLine(someText);
        }
    }
}

If you want to know more about the Reading/Writing/Deleting of files in IsolatedStorage, a good resource is http://www.windowsphonegeek.com/tips/All-about-WP7-Isolated-Storage—File-manipulations on WindowsPhoneGeek.com

PhoneApplicationService.Current.State on Windows Phone

At times you will want to store data temporary to be used in the current user “session”, but not necessarily persist it to the phone’s storage. In this case you can use PhoneApplicationService.Current.State to save and load data that will be available in the current session. To do this, I use three simple helper classes in my projects (again, I apologize for not remembering the original source for these):

public static void Clear(string key)
{
    if (PhoneApplicationService.Current.State.ContainsKey(key))
        PhoneApplicationService.Current.State.Remove(key);
}

public static void Save(string key, object value)
{
    if (PhoneApplicationService.Current.State.ContainsKey(key))
        PhoneApplicationService.Current.State.Remove(key);

    PhoneApplicationService.Current.State.Add(key, value);
}

public static T Load<T>(string key)
{
    object result;

    if (!PhoneApplicationService.Current.State.TryGetValue(key, out result))
        result = default(T);

    return (T)result;
}

RoamingSettings on Windows 8

Windows 8 introduced the wonderful concept of roaming settings. What this means is that you as a developer get around 100kb of online roaming storage for your app’s settings. Usage of this mechanism could not be easier, as it’s pretty much the same as using the local settings on the device:

ApplicationDataContainer roamingSettings = ApplicationData.Current.RoamingSettings;

roamingSettings["somekey"] = "myvalue";
roamingSettings["otherkey"] = true;

Note that there’s no need for a Save method on Windows 8.

RoamingFolder on Windows 8

There’s also an equivalent for roaming files and folders on Windows 8. To use it, I use the Storage Helper for Windows 8 class by Simon Jackson, with some minor additions (you can find it attached at the bottom of the post). The principle is the same as on Windows Phone, but because it’s roaming you will have to keep an eye on your usage in app. To do so, you can use the ApplicationData.RoamingStorageQuota property. Note that if you don’t want to roam, simply specify the LocalFolder or change the DefaultFolder in the class to

public static StorageFolder DefaultFolder = ApplicationData.Current.LocalFolder;

Downloads

Windows 8 Storage Helper - WindowsStorageHelper.zip (133 downloads)
Windows Phone Storage Helper - WindowsPhoneStorageHelper.zip (152 downloads)

2 thoughts on “Storage on Windows 8 and Windows Phone (part 1)

  1. Pingback: Storage on Windows 8 and Windows Phone (part 1)

  2. Pingback: Storage on Windows 8 and Windows Phone (part 1)

Leave a Reply