Read and Write ObservableCollection<T> to IsolatedStorage

Read and Write ObservableCollection<T> to IsolatedStorage

How do I read and write my ObservableCollection<T> to IsolatedStorage?

This is a question that I see posted over and over. In an effort to make the answer easy to find, here is a simple generic helper class that I use.

 public class IsoStoreHelper
    {
        private static IsolatedStorageFile _isoStore;
        public static IsolatedStorageFile IsoStore
        {
            get { return _isoStore ?? (_isoStore = IsolatedStorageFile.GetUserStoreForApplication()); }
        }

        public static void SaveList<T>(string folderName, string dataName, ObservableCollection<T> dataList) where T : class
        {
            if (!IsoStore.DirectoryExists(folderName))
            {
                IsoStore.CreateDirectory(folderName);
            }

            string fileStreamName = string.Format("{0}\\{1}.dat", folderName, dataName);
            try
            {
                using (var stream = new IsolatedStorageFileStream(fileStreamName, FileMode.Create, IsoStore))
                {
                    var dcs = new DataContractSerializer(typeof(ObservableCollection<T>));
                    dcs.WriteObject(stream, dataList);
                }
            }
            catch (Exception e)
            {
            }
        }

        public static ObservableCollection<T> LoadList<T>(string folderName, string dataName) where T : class
        {
            var retval = new ObservableCollection<T>();

            string fileStreamName = string.Format("{0}\\{1}.dat", folderName, dataName);

            var isf = IsoStore;
            try
            {
                var fileStream = IsoStore.OpenFile(fileStreamName, FileMode.OpenOrCreate);
                if (fileStream.Length > 0)
                {
                    var dcs = new DataContractSerializer(typeof(ObservableCollection<T>));
                    retval = dcs.ReadObject(fileStream) as ObservableCollection<T>;
                }
            }
            catch
            {
                retval = new ObservableCollection<T>();
            }

            return retval;
        }
    }

I hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Adobe Flash player