Ask Kathleen

How-to Q&A: Can I Store History Information Locally for a Sandboxed Silverlight App?

Learn how you can store history information for the user on the local client PC in a sandboxed Silverlight 3 application.

Q: I'm writing a Silverlight 3 application to run in the sandbox. Can I store history information for the user on their local machine or do I need to pass this information back to the server for storage?

A: I'll show you how to store information on the user's local machine, but first let me warn you of the possible pitfall.

From Silverlight's perspective, the user's machine is the user's machine. It is incredibly easy for the user to delete the local cache; they just right-click any Silverlight application. There's even a handy Delete All button that lets users destroy all of their caches. If you have convenience information that makes the user's life easier, local isolated storage is a great solution. If you have information that you need to protect from deletion, pass it back to the server and keep it there.

On the server you can also control backups, bridge across a client machine rebuild, and even allow the user to access the information from any machine.

Assuming the local cache makes sense in your scenario, there's a specific API for client-side storage. The isolated storage file is a type that wraps access to the isolated storage. It's quite similar to the isolated storage for the Microsoft .NET Framework, but has variations in method names and other details. The first step is to create the storage wrapper, which is an instance of the Isolated-StorageFile class:

         Using store As IsolatedStorageFile =
Isolated-StorageFile.GetUserStoreForApplication()
Dim fileName = GetSearchFileName(store)

I'll return to how I use the GetSearchFileName method, which I implemented as part of the sample. As in most other file IO approaches, you'll test that the file exists, open a stream and wrap it in a stream reader:

If store.FileExists(fileName) Then
Try
Dim isolatedStream = store.OpenFile(fileName,
FileMode.Open, FileAccess.Read)
Using reader As StreamReader = New
StreamReader(isolatedStream)

You can store any type of information, including XML data, which is convenient for history data. After checking the file position to ensure the file is not empty, you can load into an XDocument. It's XML, so I'm using Visual Basic to make parsing the XML tree easier:

   If Not reader.EndOfStream Then
Dim xDocument = XElement.Load(reader)
For Each item In xDocument.<history>
If Not String.IsNullOrEmpty(item.
-<historyItem>.@historyInfo) Then
'Do something
End If
Next
End If
End Using

If there's a failure, you'll want to catch the exception and do something appropriate in the context of your application:

Catch ex As IsolatedStorageException
' TODO: Do something intelligent
Throw
End Try
End If
End Using

Writing data to isolated storage is similar.

Obviously, you need to use the same name for reading and writing the file. If the filename includes a directory path, you need to ensure this path exists. You can do these tasks in a method to allow reuse and ensure consistency:

   Private Function GetSearchFileName(ByVal store As 
IsolatedStorageFile) As String
If Not store.DirectoryExists("Test") Then
store.CreateDirectory("Test")
End If
Return IO.Path.Combine(location, _fileName)
End Function

Silverlight 4 also offers the capacity to access the user directories on the client machine (such as My Documents) when the application runs out of the browser with elevated trust.

Read this month's next Ask Kathleen question: Value Converters in Silverlight

About the Author

Kathleen is a consultant, author, trainer and speaker. She’s been a Microsoft MVP for 10 years and is an active member of the INETA Speaker’s Bureau where she receives high marks for her talks. She wrote "Code Generation in Microsoft .NET" (Apress) and often speaks at industry conferences and local user groups around the U.S. Kathleen is the founder and principal of GenDotNet and continues to research code generation and metadata as well as leveraging new technologies springing forth in .NET 3.5. Her passion is helping programmers be smarter in how they develop and consume the range of new technologies, but at the end of the day, she’s a coder writing applications just like you. Reach her at [email protected].

comments powered by Disqus

Featured

Subscribe on YouTube