MSDN Magazine - March 2009 - (Page 16) The isolated storage APIs used here are located in the System. IO.IsolatedStorage namespace. You can get an instance of the application store by calling IsolatedStorageFile.GetUserStoreForApplication. If isolated storage is enabled (it is by default when Silverlight is installed), this will return an IsolatedStorageFile instance that can be used to work with the store. If the end user has disabled isolated storage, an IsolatedStorageException is thrown. End users can enable/disable isolated storage, so it is important to always wrap the call to IsolatedStorageFile.GetUserStoreForApplication inside a try/catch block. In fact, IsolatedStorageException could be thrown from any isolated storage operation. This could happen if the end user deletes isolated storage while the application is using it (the next operation will result in an IsolatedStorageException). Plus, if multiple instances of the application are running (in multiple browser windows or tabs, for example) and one of the instances deletes the store, then the next isolated storage operation performed by one of the other application instances will throw an IsolatedStorageException. The best practice is to wrap all calls to isolated storage inside try/catch blocks to guard against IsolatedStorageExceptions. I’ve wrapped this first code sample inside a try/catch block to illustrate this, but to keep things simple, the rest of the code samples will not show the try/catch block. Please consider all remaining code samples as being implicitly wrapped in a try/catch block. (For a rundown of best practices, see the sidebar “Silverlight Isolated Storage Best Practices.”) Now that you’ve seen the application store, let’s look at the site store. The site store is a shared store to which all Silverlight applications from the same site have access. This enables applications from the same site to share data between them. The same way Microsoft Office has common preferences across its suite of applications, you could imagine developing a suite of Silverlight applications that are all hosted on the same site and Figure 1 Location of Isolated Storage in the File System Operating System Windows Vista Windows XP Mac OS X Location in File System share a common set of preferences stored on the client. The site store is based on the identity of the user and the identity of the site. The site identity of a Silverlight application is similar to the application identity, but it only includes the scheme, host, and port of the URL, without the path to the XAP file. Like the application identity, the site identity is also case insensitive. The shared site store is new in Silverlight and is not available in the .NET Framework. IsolatedStorageFile.GetUserStoreForSite can be used to get an instance of the site store. The following code shows this in action: using (var store = IsolatedStorageFile.GetUserStoreForSite()) using (var stream = store.CreateFile("sharedhello.txt")) using (var writer = new StreamWriter(stream)) { writer.Write ("Hello Shared World"); } Another application with the same site identity could read the shared file with the following code: using (var store = IsolatedStorageFile.GetUserStoreForSite()) { if (store.FileExists("sharedhello.txt")) { using (var stream = store.OpenFile("sharedhello.txt", FileMode.Open)) using (var reader = new StreamReader(stream)) { string contents = reader.ReadToEnd(); // "Hello Shared World" } } } To better understand how a given URL to a Silverlight application maps to an application identity and site identity, refer to Figure 2. As you can see, even though the first four URLs are different, they all map to the same application identity and site identity. This is due to the fact that identities are case insensitive, query strings are not included, and port is the default port for the HTTP scheme. However, if another port is specified, such as , then the identity of the application is considered different. If the leading “www” from the host is omitted, the application identity is considered different because the host is different. The secure HTTPS URL will also yield a different identity because the scheme is different (HTTPS instead of HTTP). The last URL listed in Figure 2 will have a different application identity than the \Users\ \AppData\LocalLow\Microsoft\Silverlight\is \Documents and Settings\ \Local Settings\Application Data\Microsoft\Silverlight\is /Users/ /Library/Application Support/Microsoft/Silverlight/is Figure 2 Silverlight Application URL and Associated Application and Site Identities Application URL http://www.microsoft.com/app.xap http://www.MICROSOFT.COM/app.XAP http://www.microsoft.com/app.xap?foo=bar http://www.microsoft.com:80/app.xap http://www.microsoft.com:8080/app.xap http://microsoft.com/app.xap https://www.microsoft.com/app.xap http://www.microsoft.com/demo.xap 16 msdn magazine Application Identity HTTP://WWW.MICROSOFT.COM/APP.XAP HTTP://WWW.MICROSOFT.COM/APP.XAP HTTP://WWW.MICROSOFT.COM/APP.XAP HTTP://WWW.MICROSOFT.COM/APP.XAP HTTP://WWW.MICROSOFT.COM:8080/APP.XAP HTTP://MICROSOFT.COM/APP.XAP HTTPS://WWW.MICROSOFT.COM/APP.XAP HTTP://WWW.MICROSOFT.COM/DEMO.XAP Site Identity HTTP://WWW.MICROSOFT.COM HTTP://WWW.MICROSOFT.COM HTTP://WWW.MICROSOFT.COM HTTP://WWW.MICROSOFT.COM HTTP://WWW.MICROSOFT.COM:8080 HTTP://MICROSOFT.COM HTTPS://WWW.MICROSOFT.COM HTTP://WWW.MICROSOFT.COM CLR Inside Out
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.