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.

The code below has been tested and used with Silverlight 3 and Silverlight 4.

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();
		}
	}
}

Comments (12) -

Rod
Rod
11/13/2010 9:42:32 PM #

Thanks for sharing - is there any limit to the size of the data in the settings? I am trying to save a complex type and when I view the _LocalSettings file on the file system it is 16k and truncated halfway through some data? This caused an exception on the next read...

Reply

jim mccurdy
jim mccurdy
11/14/2010 9:52:52 AM #

Rod,

I just confirmed tha there is NOT a 16K limit on an individual file.  Let me try some experiments and see if I can duplicate your issue.

Jim

Reply

Rod
Rod
11/14/2010 3:28:25 PM #

Thanks Jim. My problem is very wierd - so it turns out that one of my base types (incidentally the MVVM Light ViewModelBase) does not have a public paramaterless constructor and is therefore not serializable.

When I add the type to the setting, it seems to add (I watch the filesize of _LocalSettings increase) but when you try to access ANY setting on the next load (after you exit the program) it falls over OR just blanks the file back to empty.

As soon as I made the base class serializable then it worked again. More info here- stackoverflow.com/.../is-there-a-size-limit-of-16k-for-silverlight-localsettings-isolatedstoragesettin

Reply

MAGESH
MAGESH
5/5/2011 5:01:07 AM #

Hi ,

I'm new to lamda expression..Can you pls tell me how to call the read method..bcoz i dunno how to call the read method.

Reply

Jim McCurdy
Jim McCurdy
5/5/2011 12:03:03 PM #

Magesh,

There are no lambda expression used here.  Do you mean generics (as in Read<T>) ?

To call the read method, pass the type of the setting like so:

// Save the most recent filename setting
string writeFileName = "MyDocument.xml";
Settings.Write<string>("MostRecentFileName", writeFileName);

// Restore the most recent filename setting
string readFileName = Settings.Read<string>("MostRecentFileName", "Untitled.xml");

Reply

MAGESH
MAGESH
5/6/2011 2:14:56 AM #

Hi Jim..

Thanks for ur update.

Reply

webdesigner
webdesigner
5/18/2011 7:52:01 AM #

the code is easy but useful.thanks

Reply

Web designer
Web designer
8/12/2011 10:40:13 AM #

Nice. So we can use the same code for Silverlight 3 and Silverlight 4. Interesting.

Reply

Storage Sydney
Storage Sydney
11/2/2011 7:59:17 PM #

A storage setting? The idea is very helpful with the fact that it is applicable to Silverlight 3 and 4. And the code was made easy for us to encode, good thing that such application preferences can be now stored. Thanks a bunch for this.

Reply

Ralph Lauren Outlet
Ralph Lauren Outlet
2/4/2012 2:32:49 AM #

http://www.poloralphlauren-store.com/Very glad to read your article.Your blog which contains much knowledge I've never heard.And your article also reveals a lot of meaningful knowledge.

Reply

Ralph Lauren Outlet Online
Ralph Lauren Outlet Online
2/4/2012 2:37:55 AM #

http://www.poloralphlauren-store.com/I agree on you opionion. Just take part in this subject talk and learn a lot from it.

Reply

Ralph Lauren Outlet Online
Ralph Lauren Outlet Online
2/4/2012 2:40:50 AM #

http://www.poloralphlauren-store.com/This is very nice blog and the information you offer are really nice and useful and i would like to visit the blog again.

Reply

Pingbacks and trackbacks (3)+

Add comment

biuquote
  • Comment
  • Preview
Loading

About Me

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

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 contract development projects for Silverlight, WPF, and ASP.NET platforms. Recent projects include two complete web sites developed using Silverlight, .NET, and C#. The first is YinYangMoney.com, a unique financial and lifestyle planning web application. The second is McPivot.com, a unique web application that presents custom queries and visualization of baseball data back to the 1800's.

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.

Blog Archive

Welcome Readers