Skip to main content

Microsoft Silverlight

MicrosoftWritten by:
Microsoft
Microsoft

Isolated Storage

0 0

Summary

Describes how to save and retrieve data as key-value pairs or files in isolated storage.

In Silverlight, there is no direct access to the operating system's file system, except through the Open File dialog box. However, you can use isolated storage to store data locally on the user's computer. There are two ways to use isolated storage. The first way is to save or retrieve data as key/value pairs by using the IsolatedStorageSettings class. The second way is to save or retrieve files by using the IsolatedStorageFile class. This QuickStart demonstrates both storage techniques.

This QuickStart contains the following sections:

For a complete description of isolated storage, see Isolated Storage in the Silverlight documentation on MSDN.


Isolated Storage Example

The following example demonstrates how to save a user theme in isolated storage. The default theme is "Blue Hills". To try this example, select another theme to save it in isolated storage. This theme will be displayed whenever you run this application.

To remove the setting and the image file from isolated storage, click Remove Isolated Storage Data. Clicking the Refresh tab or refreshing your browser will reset the theme to "Blue Hills".

Based on the user's selection, the name of an image and the actual image file itself are saved in isolated storage. The image name is saved in a setting named userSettings with a key of userImage. Saving the image name is not required, but it is saved as a convenience. The image file is saved in a directory named Images and is named UserImageFile.jpg. The following illustration shows the data that is saved in isolated storage.

Isolated Storage illustration

Three .jpg image files are compiled as resources for the example. The BlueHills.jpg image is established as the default theme. The example obtains the stream of the resource file and writes it to UserImageFile.jpg in isolated storage. That file is overwritten whenever a different theme is selected from the list box.


Working with Key/Value Pairs

To save or retrieve data as key/value pairs, you use the IsolatedStorageSettings class. IsolatedStorageSettings is a dictionary that enables you to store strings and other objects. You typically use the following methods on IsolatedStorageSettings:

  • Add: Adds an entry to the dictionary for the key/value pair.
  • Contains: Determines whether the dictionary contains the specified key.
  • Remove: Removes the entry with the specified key.

To get or set the value that is associated with the specified key, you use the Item property. You typically use the following syntax. If the key does not already exist, a new entry is added to the dictionary.

myCollection["myKey"] = myValue (C#) myCollection("myKey") = myValue (Visual Basic)

The following code demonstrates how to add and remove a key/value pair.

C#

// Create an instance of IsolatedStorageSettings. private IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings; // Add a key and its value. userSettings.Add("userImage", "BlueHills.jpg"); // Remove the setting. userSettings.Remove("userImage");

Visual Basic

' Create an instance of IsolatedStorageSettings. Private userSettings As IsolatedStorageSettings = _ IsolatedStorageSettings.ApplicationSettings ' Add a key and its value. userSettings.Add("userImage", "BlueHills.jpg") ' Remove the setting. userSettings.Remove("userImage")

Working with Files

To save or retrieve files, you use the IsolatedStorageFile class. IsolatedStorageFile represents an isolated storage area that contains files and directories. When you work with files, you also typically use the IsolatedStorageFileStream class to read and write content to the file. You typically use the following methods on IsolatedStorageFile:

  • GetUserStoreForApplication: Obtains user-scoped isolated storage that corresponds to the calling code's application identity.
  • FileExists: Determines whether the specified path refers to an existing file in the isolated store.
  • CreateFile: Creates a file in the isolated store.
  • OpenFile: Opens a file at a specified path using the specified sharing and access options. This method returns an IsolatedStorageFileStream object that contains the file's stream.
  • DeleteFile: Deletes a file in the isolated store.
  • DirectoryExists: Determines whether the specified path refers to an existing directory in the isolated store.
  • CreateDirectory: Creates a directory in the isolated storage scope.
  • DeleteDirectory: Deletes a directory in the isolated storage scope.
  • Remove: Removes the isolated storage scope and all its contents.
Because isolated storage uses unmanaged resources, an instance of the IsolatedStorageFile class and the IsolatedStorageFileStream class should be disposed after their use. The using statement does this for you automatically and is a good practice to use. The following code demonstrates some of the IsolatedStorageFile and IsolatedStorageFileStream methods.

Note

To download all the code for this example, see Isolated Storage example download.

C#

// Obtain the isolated storage for an application. using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { // Create a directory at the root of the store. if (!store.DirectoryExists("Images")) { store.CreateDirectory("Images"); } // Get the stream of an existing file, // or create a file and get its stream. using (var isoStream = store.OpenFile(@"Images\UserImageFile.jpg", FileMode.Open)) { IsolatedStorageFileStream isoStream = store.OpenFile( @"Images\UserImageFile.jpg", FileMode.OpenOrCreate); ... // Write to the stream. isoStream.Write(bytes, 0, numBytesToRead); } ... // Remove isolated storage. store.Remove(); }

Visual Basic

' Obtain the isolated storage for an application. Using store As IsolatedStorageFile = _ IsolatedStorageFile.GetUserStoreForApplication() ' Create a directory at the root of the store. If Not store.DirectoryExists("Images") = True Then store.CreateDirectory("Images") End If ' Get the stream of an existing file, ' or create a file and get its stream. Using isoStream As IsolatedStorageFileStream = _ store.OpenFile("Images\UserImageFile.jpg", FileMode.OpenOrCreate) ... ' Write to the stream. isoStream.Write(bytes, 0, numbytesToRead) End Using ... ' Remove isolated storage data. store.Remove() End Using

See Also


Send Feedback

Leave a Comment Comments (0) RSS Feed

  • 1

You must be logged in to leave a comment. Click here to log in.

Quickstarts

Microsoft Communities