Article ID: 2127835 - Last Review: July 20, 2010 - Revision: 2.0

How to use Isolated Storage in Silverlight 3

System TipThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.

On This Page

Expand all | Collapse all

INTRODUCTION

This article describes an All-In-One Code Framework sample that is available for download. Isolated storage enables applications to create and maintain a safe client-side virtual file system for partial trust applications. In Silverlight, all I/O operations are restricted to isolated storage and do not use the file system of the operating system. This sample demonstrates how to utilize isolated storage to save and load bytes from isolated storage.  It also mentions how to improve the application performance by using BackgroundWorker.

Difficulty level

Collapse this imageExpand this image

Download information

To download this code sample, click the following link:

  


Technical overview

  • How to store files to isolated storage?
    1. Use OpenFileDialog to get a local readable file stream.
    2. Get IsolatedStorageFile from application, and then use CreateFile method to get one writeable IsolatedStorageStream.
    3. Copy bytes from file stream to IsolatedStorageStream.
    4. Close stream.
How to store files to isolated storage?
Note: copy long stream is very time consuming, use BackgroundWorker to run it in other thread could get better performance. Details about BackgroundWorker, see

       http://msdn.microsoft.com/en-us/library/cc221403(VS.95).aspx (http://msdn.microsoft.com/en-us/library/cc221403(VS.95).aspx)
  • How does isolated storage treeview viewmodel works?
    1. Define entities for isolated storage virtual file and directory.
    2. In treeview, use HierarchicalDataTemplate to bind data to treeview node.
    3. When application starts, traverse isolated storage, use defined entity to create treeview viewmodel, and then set it as treeview's itemssource.
    Sample code:

    Entity definition(C#)

       // Isolatedstoarge file obj

        public class IsoFile

        {

            public string FilePath{set;get;}

            public string FileName { set; get; }



            public Stream ContentStream

            {

                private set;

                get;

            }

            public IsoFile(string filename, string path)

            {

                FileName = filename;

                FilePath = path;

            }

        }



        // Isolatedstorage directory obj

        public class IsoDirectory:IsoFile

        {

            public ObservableCollection<IsoFile> Children

            {

                private set;

                get;

            }

            public IsoDirectory(string filename,string path)

                : base(filename,path)

            {

                Children = new ObservableCollection<IsoFile>();

            }

    }
    Entity definition(VB.net)

    ' Isolatedstoarge file obj

    Public Class IsoFile

        Private _FilePath As String

        Public Property FilePath() As String

            Get

                Return _FilePath

            End Get

            Set(ByVal value As String)

                _FilePath = value

            End Set

        End Property

        Private _FileName As String

        Public Property FileName() As String

            Get

                Return _FileName

            End Get

            Set(ByVal value As String)

                _FileName = value

            End Set

        End Property



    ' Isolatedstorage directory obj

    Public Class IsoDirectory

        Inherits IsoFile

        Private _Children As ObservableCollection(Of IsoFile)

        Public Property Children() As ObservableCollection(Of IsoFile)

            Get

                Return _Children

            End Get

            Private Set(ByVal value As ObservableCollection(Of IsoFile))

                _Children = value

            End Set

        End Property

        Public Sub New(ByVal filename As String, ByVal path As String)

            MyBase.New(filename, path)

            Children = New ObservableCollection(Of IsoFile)()

        End Sub

    End Class
    Xaml
    <controls:TreeView Name="tv1"


                                                                                                               SelectedItemChanged="TreeView_SelectedItemChanged">

                            <controls:TreeView.ItemTemplate>

                                <win:HierarchicalDataTemplate ItemsSource="{Binding Children}">

                                    <StackPanel Orientation="Horizontal">

                                        <Image HorizontalAlignment="Left" Width="16" Height="16"    Source="{Binding Path=.,Converter={StaticResource ic1}}" />

                                        <TextBlock Text="{Binding FileName}" />

                                    </StackPanel>

                                </win:HierarchicalDataTemplate>

                            </controls:TreeView.ItemTemplate>

       </controls:TreeView>

  • How to open and play media stream stored in isolated storage?
    1. Open isolated storage stream.
      IsolatedStorageFile _isofile = IsolatedStorageFile.GetUserStoreForApplication();       
      var stream = _isofile.OpenFile(item.FilePath, FileMode.Open, FileAccess.Read);
    2. Set it to MediaElement, then play.
      player1.SetSource(stream);
  • How to use IsolatedStorageSetting to store config?

    IsolatedStorageSettings is a dictionary stored in isolated storage and maintained by Silverlight. It's a good place to store configuration.

        To store data to IsolatedStorageSetting for application usage, use
    IsolatedStorageSettings. ApplicationSettings["keyname"] = data;


        And if need store data for whole site usage, use
    IsolatedStorageSettings. SiteSettings["keyname"] = data;

  • Why directory depth should be less than 4 in application?

    Isolated storage limit the directory name to be less than 248 characters, and the full file path to be less than 260 characters. If create deeper directory, the path length would exceed the limitation. 


NoteFor more information about how to create the sample application and how to deploy the sample application, see the Readme.txt file that is included in the download package.

Technology category

  • Silverlight

Languages

This code sample contains the following programming language:
Collapse this tableExpand this table
LanguageProject Name
Visual C#CSSL3IsolatedStorage
Visual Basic.NETVBSL3IsolatedStorage



Prerequisites

Tag

  • IsolatedStorage, Silverlight

MORE INFORMATION

What is All-In-One Code Framework?

All-In-One Code Framework shows most Microsoft development techniques by using code samples in different programming languages. Each example is carefully selected, composed, and documented to show one common code scenario. For more information about All-In-One Code Framework, visit the following Web site:
http://1code.codeplex.com (http://1code.codeplex.com)

How to find more All-In-One Code Framework samples

To find more All-In-One Code Framework samples, you can simply click the following link:
http://support.microsoft.com/gp/gp_kbcodefx (http://support.microsoft.com/gp/gp_kbcodefx)

REFERENCES

For more information, visit the following Web sites:

Isolated Storage http://msdn.microsoft.com/en-us/library/bdts8hk0(VS.95).aspx (http://msdn.microsoft.com/en-us/library/bdts8hk0(VS.95).aspx)



Note This is a "FAST PUBLISH" article created directly from within the Microsoft support organization. The information contained herein is provided as-is in response to emerging issues. As a result of the speed in making it available, the materials may include typographical errors and may be revised at any time without notice. See Terms of Use (http://go.microsoft.com/fwlink/?LinkId=151500) for other considerations.

APPLIES TO
  • Microsoft Visual Studio 2008 Service Pack 1
Keywords: 
kbcodefx kbrapidpub kbnomt atdownload kbsurveynew kbinfo KB2127835