Settings for Silverlight using IsolatedStorageSettings

by Jim McCurdy 14. August 2009 22:34

Here is a simple static class, Settings, that offers methods to Read and Write application settings and preferences from Silverlight applications.  These settings are similar to the ones that Windows apps would store in the registry or in an INI file, but in this case, are written to Silverlight’s isolated storage.

This class wraps Silverlight’s IsolatedStorageSettings class, so remember; since a user has the freedom to purge isolated storage at will, this class is mostly useful for storing application preferences like layout sizes and locations, and other creature comforts for regular users.  This class can read or write any data type to settings storage; the IsolatedStorageSettings class will automatically serialize and deserialize the settings for you.

This code below works with Silverlight 3.

using System.IO.IsolatedStorage;

namespace ClassLibrary
{
	public static class Settings
	{
		public static TT Read<TT>(string name)
		{
			return Read<TT>(name, default(TT));
		}

		public static TT Read<TT>(string name, TT defaultValue)
		{
			IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
			TT value;
			if (settings == null || !settings.TryGetValue<TT>(name, out value))
				return defaultValue;
			return value;
		}

		public static void Write<TT>(string name, TT value)
		{
			IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
			if (settings == null)
				return;
			if (settings.Contains(name))
				settings[name] = value;
			else
				settings.Add(name, value);
			settings.Save();
		}
	}
}
Digg It!DZone It!Del.icio.usStumbleUpon

Comments

1/29/2010 9:41:25 AM #

Settings for Silverlight using IsolatedStorageSettings

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

2/1/2010 11:58:32 AM #

Settings for Silverlight using IsolatedStorageSettings

Thank you for submitting this cool story - Trackback from DotNetShoutout

DotNetShoutout

Add comment




biuquote
Loading



Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen


My Photo Jim is always looking for new clients in need of software development expertise.  He is particularly well suited for Silverlight, .NET, ASP.NET, and WPF projects.

Professional Biography

Jim McCurdy operates Face to Face Software, where he works designing, developing, and managing software projects for a variety of clients.

Jim currently specializes in development projects for Silverlight, .NET, ASP.NET, and WPF platforms. One such project is a complete web site using Silverlight, .NET, and C#; a unique financial and lifestyle planning web application at YinYangMoney.com.

Jim has worked in the software industry for as long as he can remember, and has broad expertise in Web, Windows, and systems technologies. Jim has been a team player on many agile development projects throughout his career, and is a founding member at software startups Astral Development and Powerhouse Entertainment.

You can Email Jim at Face to Face Software.


Welcome Readers