StorageHelper - For Windows Phone + Silverlight app developers


Few months ago, I wrote a small helper class for all my Windows Phone Silverlight applications, which helped me a lot to store and retrieve data to and from IsolatedStorageSettings. Thus I thought to share the same with you, in case you find it useful.

 

Read more to  find out the code, which you can use freely. Here the only catch is: it works only with the Silverlight type applications.


 

 

The helper class consists of three methods named “StoreSetting”, “GetSetting” and “RemoveSetting”. As the name suggests, the said methods will help you to save, retrieve and delete your data to and from Isolated Storage Settings.

 

Please find below the complete source code which will work with Windows Phone Silverlight type applications, but I will try to provide another code snippet quite soon to help you work with Universal apps.

 

namespace KunalChowdhury.Helpers
{
    using System.IO.IsolatedStorage;
 
    public class StorageHelper
    {
        public static bool StoreSetting(string key, object value)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
            {
                IsolatedStorageSettings.ApplicationSettings[key] = value;
                return true;
            }
 
            IsolatedStorageSettings.ApplicationSettings.Add(key, value);
            return true;
        }
 
        public static T GetSetting<T>(string key, T defaultVal = default(T))
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
            {
                return (T)IsolatedStorageSettings.ApplicationSettings[key];
            }
 
            return defaultVal;
        }
 
        public static void RemoveSetting(string key)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
            {
                IsolatedStorageSettings.ApplicationSettings.Remove(key);
            }
        }
    }
}

 

I hope, this optimized code will help you to better utilize your data in Isolated Storage Settings. Don’t forget to check out my other Windows Phone related articles.

 

Connect with me over Twitter, Facebook and Google+. Subscribe to my blog’s RSS feed and Email Newsletter to get immediate updates about my posts in your inbox. Happy Coding. Cheers.

 


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

1 comments

  1. Kunal,
    Can you please help me and let me know which software you are using to generate images?
    Image I am talking seems to be merger to 3 images - "File Image", "Code Image" and "Phone Image".

    Thanks,
    Varun Khanna

    ReplyDelete

 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top