using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Deployment.Application;// Need to add reference to System.Deployment.dll framework assembly
namespace IDesign.Utilities
{
public static class IsoStoreSettingsHelper
{
/// <summary>
/// Reads a setting from isolated storage.
/// Assumes isolation by domain for non-ClickOnce deployed apps.
/// Assumes isolation by application for ClickOnce deployed apps.
/// </summary>
/// <param name="fileName">The name of the settings file to use.</param>
/// <param name="key">The key name of the setting to look up.</param>
/// <returns>The value stored in the file if found, the default value for the type otherwise.</returns>
public static T Read<K,T>(string fileName,K key)
{
try
{
IsolatedStorageFile isoFile = OpenIsoStoreFile(fileName);
string[] files = isoFile.GetFileNames(fileName);
if(files.Length == 0 || files[0].Length == 0)
{
// File does not exist, return default
return default(T);
}
// File exists, deserialize the settings
using(Stream stream = new IsolatedStorageFileStream(fileName, FileMode.Open, isoFile))
{
XmlSerializableHashtable settings = XmlSerializableHashtable.Load(stream);
if(settings == null)
{
return default(T);
}
return (T)settings[key];
}
}
catch
{}
return default(T);
}
/// <summary>
/// Write a setting to an isolated storage setting file.
/// Assumes isolation by domain for non-ClickOnce deployed apps.
/// Assumes isolation by application for ClickOnce deployed apps.
/// </summary>
/// <param name="fileName">The name of the settings file to use.</param>
/// <param name="key">The key name of the setting to look up.</param>
/// <param name="value">The value of the object to store.</param>
public static void Write<K,T>(string fileName, K key, T value)
{
try
{
IsolatedStorageFile isoFile = OpenIsoStoreFile(fileName);
XmlSerializableHashtable settings = null;
// First try to read in existing settings is there are any
using(Stream stream = new IsolatedStorageFileStream(fileName, FileMode.OpenOrCreate, isoFile))
{
try
{
settings = XmlSerializableHashtable.Load(stream);
}
catch
{
}
if(settings == null)
{
settings = new XmlSerializableHashtable(); // Create empty one for new settings
}
}
//Add the new setting to the collection
settings[key] = value;
//Now write the collection back out
using(Stream writeStream = new IsolatedStorageFileStream(fileName,FileMode.Create,isoFile))
{
settings.Save(writeStream);
}
}
catch
{ }
}
static IsolatedStorageFile OpenIsoStoreFile(string fileName)
{
IsolatedStorageFile isoFile = null;
if(ApplicationDeployment.IsNetworkDeployed)
{
isoFile = IsolatedStorageFile.GetUserStoreForApplication();
}
else
{
isoFile = IsolatedStorageFile.GetUserStoreForDomain();
}
return isoFile;
}
}
}
XmlSerializableHashtable class:
using System;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace IDesign.Utilities
{
// Support storage of .NET primitives
[XmlInclude( typeof(string) )]
[XmlInclude( typeof(bool) )]
[XmlInclude( typeof(short) )]
[XmlInclude( typeof(int) )]
[XmlInclude( typeof(long) )]
[XmlInclude( typeof(float) )]
[XmlInclude( typeof(double) )]
[XmlInclude( typeof(DateTime) )]
[XmlInclude( typeof(char) )]
[XmlInclude( typeof(decimal) )]
[XmlInclude( typeof(UInt16) )]
[XmlInclude( typeof(UInt32) )]
[XmlInclude( typeof(UInt64) )]
[XmlInclude( typeof(Int64) )]
public class XmlSerializableHashtable
{
#region Nested Class--Entry
/// <summary>
/// Represents an entry for the hashtable
/// </summary>
public class Entry
{
private object m_EntryKey;
private object m_EntryValue;
/// <summary>
/// Default constructor, needed by serialization support
/// </summary>
public Entry(){}
/// <summary>
/// Construct the Entity specifying the key and the entry
/// </summary>
/// <param name="entryKey"></param>
/// <param name="entryValue"></param>
public Entry( object entryKey , object entryValue )
{
m_EntryKey = entryKey;
m_EntryValue = entryValue;
}
/// <summary>
/// Return the key
/// </summary>
[XmlElement("key")]
public object EntryKey
{
get{ return m_EntryKey; }
set{ m_EntryKey = value; }
}
/// <summary>
/// Return the entry value
/// </summary>
[XmlElement("value")]
public object EntryValue
{
get{ return m_EntryValue; }
set{ m_EntryValue = value; }
}
}
#endregion
#region Declarations
private Hashtable m_HashTable;
#endregion
#region Constructors
/// <summary>
/// Default constructor
/// </summary>
public XmlSerializableHashtable()
{
m_HashTable = new Hashtable();
}
/// <summary>
/// Creates a serializable hashtable using a hashtable
/// </summary>
/// <param name="ht"></param>
public XmlSerializableHashtable( Hashtable existingHashTable )
{
m_HashTable = existingHashTable;
}
#endregion
#region Public Methods & Properties
/// <summary>
/// Indexer to access the underlying Hashtable
/// </summary>
/// <param name="key">The key for which you want to retrieve the value</param>
/// <returns></returns>
[XmlIgnore]
public object this[object key]
{
get
{
lock (m_HashTable.SyncRoot)
{
return m_HashTable[key];
}
}
set
{
lock (m_HashTable.SyncRoot)
{
m_HashTable[key] = value;
}
}
}
/// <summary>
/// Save the hashtable to an output stream
/// </summary>
/// <param name="outputStream">The stream to write to</param>
public void Save(Stream outputStream)
{
XmlSerializer serializer = new XmlSerializer(typeof(XmlSerializableHashtable));
serializer.Serialize(outputStream,this);
}
/// <summary>
/// Load a hashtable from an input stream
/// </summary>
/// <param name="inputStream">The stream from which to load</param>
/// <returns>A new instance of an XmlSerializableHashtable</returns>
public static XmlSerializableHashtable Load(Stream inputStream)
{
XmlSerializer serializer = new XmlSerializer(typeof(XmlSerializableHashtable));
return serializer.Deserialize(inputStream) as XmlSerializableHashtable;
}
/// <summary>
/// Returns the contained hashtable
/// </summary>
[XmlIgnore]
public Hashtable InnerHashtable
{
get{ return m_HashTable; }
}