D'Arcy from Winnipeg
Solution Architecture, Business & Entrepreneurship, Microsoft, and Adoption

What You Need To Know About IsolatedStorageSettings

Wednesday, September 16, 2009 11:28 PM

I’m putting together some demos for an upcoming Silverlight talk I’m giving, and one of the topics is on Isolated Storage. For those not aware, Isolated Storage is the small sandboxed area of the users computer that a Silverlight application can write to and read from.

You can access this space through reader and writer objects, but that might be overkill if you just need to set some values. In that case, the IsolatedStorageSettings class is your friend.

This class lets you treat storing values for your app like session or viewstate in ASP.NET. The syntax is dead simple:

IsolatedStorageSettings.ApplicationSettings["SettingName"] = someValue;

(Note that you can access the SiteSettings in this manner as well)

However, there’s some nuances that you should be aware of when using this object.

When you use the line of code above, what you’re doing is setting the value part of the key/value pair in the in-memory IsolatedStorageSettings object. This means that it hasn’t actually gotten committed to the isolated storage area. In fact, from the class’s MSDN article:

Data written to the IsolatedStorageSettings object is saved when the application that uses the class is closed. This can occur when the user closes the Web browser, refreshes a page, or browses away from the page. If you want your application to write to isolated storage immediately, you can call the Save method in application code.

That last bit is talking about a line of code you can use to force the saving of the values in the object to the Isolated Storage area of the computer drive:

IsolatedStorageSettings.ApplicationSettings.Save();

Now the reason knowing all this is important is because if you were to, say, try to whip up demos for an upcoming talk, you might find behavior you weren’t expecting.

For instance, I have a small app that shows off setting values as per the IsolatedStorageSettings code shown above. However, after changing the value in the first browser, I couldn’t get the changes to show up in the second. This is because changes are made to each application’s in-memory ISS ApplicationSettings object. Even if you were to force save the values to ISS, the other app in the other browser never goes back to refresh the values…any reference to the ISS.ApplicationSettings[ ] will grab values from the in-memory object.

In real life, you may not have an issue with this…most of the time, users won’t have your application open in two different browsers doing work in both at the same time. But be aware that there are some gotchas in working with the ISS object and its methods.

D




Feedback

# re: What You Need To Know About IsolatedStorageSettings

Good one Darcy.

Thanks,
Bala. 9/3/2010 4:13 AM | Balakrishna Yeluvolu

# re: What You Need To Know About IsolatedStorageSettings

It seems the IsolatedStorageSettings.ApplicationSettings data are stored in a file named _ApplicationSettings in root of isolated storage. This is important, not to create a custom file with same name. 9/7/2010 2:52 AM | v

# re: What You Need To Know About IsolatedStorageSettings

Hi Darcy,

Did you ever find a work around for the issue of ISS not refreshing the application settings values after they've been changed by a difference app instance?

I've run into that issue today and I'm stumped. 7/3/2011 3:18 PM | Josef Rogovsky

Post a comment