[This topic is pre-release documentation and is subject to change in future releases of Microsoft Silverlight.]

How to: Use Isolated Storage with .NET Framework Silverlight

Introduction

This QuickStart sample demonstrates how to obtain an isolated store, and how to create and delete files in isolated storage using the .NET Framework for Silverlight. It also shows how to save data to, and retrieve data from, isolated storage in Microsoft Silverlight.

Run View
Language:

Working with this capability in your Silverlight-based application involves the following tasks:

  • Creating a new file in isolated storage and writing to it using StreamWriter.

  • Opening an existing file in isolated storage and reading the content using StreamReader.

  • Deleting files in isolated storage.

Prerequisites (available from the Silverlight download site):

  • Microsoft Silverlight 1.1 Alpha.

  • Microsoft Visual Studio 2008 Beta 2.

  • Microsoft Silverlight Tools Alpha for Visual Studio 2008 Beta 2.

  • A Silverlight project. For instructions, see How to: Create a Silverlight Project.

Working with the IsolatedStorageFile class

The IsolatedStorageFile class abstracts the virtual file system for isolated storage. You create an instance of the IsolatedStorageFile class, and then use it to enumerate and manipulate folders and files. You also use this class when you create IsolatedStorageFileStream objects to manipulate the contents of files.

The root of the virtual file system is located in a per-user, hidden folder in the physical file system. Each unique identifier provided by the host will map to a different consistent root, giving each application its own virtual file system. The folder structure and hidden scheme that are used in .NET Framework version 2.0 are also used in the .NET Framework for Silverlight.

You must use an absolute path to reference the files in isolated storage. You must not pass in relative paths such as ..\..\..\..\filename to escape the constrained file system.

To create a new file in isolated storage and write to it using StreamWriter

  1. Obtain a user-isolated store for your application.

    CS

    using (IsolatedStorageFile isoStore =
               IsolatedStorageFile.GetUserStoreForApplication())
    
    

    VB

    Using isoStore As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
    
    
  2. Create a new file named IsoStoreFile.txt in the root directory.

    CS

    using (IsolatedStorageFileStream isoStream =
        new IsolatedStorageFileStream("IsoStoreFile.txt",
            FileMode.Create, isoStore))
    
    

    VB

    Using isoStream As New IsolatedStorageFileStream("IsoStoreFile.txt", FileMode.Create, isoStore)
    
    
  3. Write to the file.

    using (StreamWriter writer = new StreamWriter(isoStream))
    {
        writer.Write("This is isolated storage file.");
    }

    Using writer As New StreamWriter(isoStream)
        writer.Write("This is isolated storage file.")
    End Using

    You do not have to call the Close method for isoStore, isoStream, and writer because the using statement defines a scope outside of which objects will be disposed.

To open an existing file in isolated storage and read its contents using StreamReader

  1. Open an existing isolated storage file.

    CS

    using (IsolatedStorageFileStream isoStream =
        new IsolatedStorageFileStream("IsoStoreFile.txt",
            FileMode.Open, isoStore))
    
    

    VB

    Using isoStream As New IsolatedStorageFileStream("IsoStoreFile.txt", FileMode.Open, isoStore)
    
    
  2. Read the contents of the file by using StreamReader.

    using (StreamReader reader = new StreamReader(isoStream))
    {
        // Read the first line from the file.
        String sb = reader.ReadLine();
    }

    Using reader As New StreamReader(isoStream)
        ' Read a line from the file.
        Dim sb As String = reader.ReadLine()
    End Using

    You do not have to call the Close method for the reader object because the using statement defines a scope outside of which objects will be disposed.

Delete a file from isolated storage

  1. Call the DeleteFile() method.

    CS

    isoStore.DeleteFile("IsoStoreFile.txt");
    
    

    VB

    isoStore.DeleteFile("IsoStoreFile.txt")