Jim McCurdy's Tech Blog

Insights into C#/XAML Development; Windows 8 (WinRT), WPF, Windows Phone, Xamarin, Silverlight

Settings for Silverlight using IsolatedStorageSettings

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)
			if (settings.Contains(name))
				settings[name] = value;
				settings.Add(name, value);

Comments (9) -

  • 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...

    • jim mccurdy

      11/14/2010 9:52:52 AM |


      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.


  • 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


    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.

    • Jim McCurdy

      5/5/2011 12:03:03 PM |


      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");

      • MAGESH

        5/6/2011 2:14:56 AM |

        Hi Jim..

        Thanks for ur update.

  • webdesigner

    5/18/2011 7:52:01 AM |

    the code is easy but useful.thanks

  • Web designer

    8/12/2011 10:40:13 AM |

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

  • 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.

Comments are closed