DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Persisting Application Data Using Hashtable And IsolatedStorage

10.03.2006
| 4813 views |
  • submit to reddit
        // nice way to store application data and settings
// copied from: http://www.dotnetspider.com/kb/Article344.aspx

using System;
using System.Collections;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace CustomStorage
{
    [Serializable]
    public class ApplicationStorage : Hashtable
    {
        // File name. Let us use the entry assembly name with .dat as the extension.
        private string settingsFileName = 
                        System.Reflection.Assembly.GetEntryAssembly().GetName().Name + ".dat";
    
        // The default constructor.
        public ApplicationStorage()
        {
            LoadData();
        }
        
        // This constructor is required for deserializing our class from persistent storage.
        protected ApplicationStorage (SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }
        
        private void LoadData()
        {
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore( IsolatedStorageScope.User 
                        | IsolatedStorageScope.Assembly, null, null );
            if ( isoStore.GetFileNames( settingsFileName ).Length == 0 )
            {
                // File not exists. Let us NOT try to DeSerialize it.
                return;
            }
            
            // Read the stream from Isolated Storage.
            Stream stream = new IsolatedStorageFileStream( settingsFileName, 
                        FileMode.OpenOrCreate, isoStore );
            if ( stream != null )
            {
                try
                {
                    // DeSerialize the Hashtable from stream.
                    IFormatter formatter = new BinaryFormatter();
                    Hashtable appData = ( Hashtable ) formatter.Deserialize(stream);
                    
                    // Enumerate through the collection and load our base Hashtable.
                    IDictionaryEnumerator enumerator = appData.GetEnumerator();
                    while ( enumerator.MoveNext() )
                    {
                        this[enumerator.Key] = enumerator.Value;
                    }
                }
                finally
                {
                    // We are done with it.
                    stream.Close();
                }
            }
        }
        
        public void ReLoad()
        {
            LoadData();
        }
        
        // Saves the configuration data to the persistent storage.
        public void Save()
        {
            // Open the stream from the IsolatedStorage.
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore( IsolatedStorageScope.User 
                        | IsolatedStorageScope.Assembly, null, null );
            Stream stream = new IsolatedStorageFileStream( settingsFileName, 
                        FileMode.Create, isoStore );
        
            if ( stream != null )
            {
                try
                {
                    // Serialize the Hashtable into the IsolatedStorage.
                    IFormatter formatter = new BinaryFormatter();
                    formatter.Serialize( stream, (Hashtable)this );
                }
                finally
                {
                    stream.Close();
                }
            }
        }
    }
}

//sample usage

CustomStorage.ApplicationStorage storage = new CustomStorage.ApplicationStorage();

storage["name"] = "john";
storage["age"] = 23;
storage["address"] = "#10, Vasant Nagar";

storage.Save();

string name = storage["name"].ToString();
int age = int.parse(storage["age"].ToString());