web 2.0

Store and Read Files in Client's Computer Using Isolated Storage

Says your application downloads some data files and need to save them locally in the client's computer for subsequent uses. Well, Silverlight in partial-trust mode cannot access client's file system directly, because that would seriously compromise the client's computer. So, Silverlight team devised an ingenious solution by creating a sandboxed virtual file system as known as Isolated Storage. As the name implies, each Silverlight application is given an isolated storage for its own use, up to about 1MB in Silverlight 1.1 Alpha. This opens up a lot of good possibilities like saving user settings, temporary files, form inputs, etc. Without further ado, let's see how to save a text file:

    Shared Sub StoreIsolatedFile(ByVal path As String, ByVal data As String)
        Dim iso As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
        Using isoStream As New IsolatedStorageFileStream(path, IO.FileMode.Create, iso)
            Using writer As New IO.StreamWriter(isoStream)
                writer.Write(data)
            End Using
        End Using
    End Sub

As you can see, the very thing to do is get the isolated storage for the application by calling IsolatedStorageFile.GetUserStoreForApplication(). Then you'll need to create an IsolatedStorageFileStream to create a file and write the data. It's easy, isn't? Note that we don't need to call isoStream.Close() here, because Using...End Using statement will take care of it. Now let's take a look at this snippet on how to read a text file from the storage:

    Shared Function ReadIsolatedFile(ByVal path As String) As String
        Dim iso As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
        Using isoStream As New IsolatedStorageFileStream(path, IO.FileMode.Open, iso)
            Using reader As New IO.StreamReader(isoStream)
                Return reader.ReadToEnd
            End Using
        End Using
    End Function

Again, firstly we get the isolated storage for the application. And we use IsolatedStorageFileStream to open a file and read its data. The StreamReader is just a wrapper stream to read string more easily. Ok, take a look at how to use above snippets to write some string to test.txt file and read the string back:

StoreIsolatedFile("test.txt", "SilverlightExamples.NET rocks!")
Dim data as String = ReadIsolatedFile("test.txt") 


For C# developers, you can use below C# snippets:

    public static void StoreIsolatedFile(string path, string data)
    {
        IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
        using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(path, IO.FileMode.Create, iso)) {
            using (IO.StreamWriter writer = new IO.StreamWriter(isoStream)) {
                writer.Write(data);
            }
        }
    }

    public static string ReadIsolatedFile(string path)
    {
        IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
        using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(path, IO.FileMode.Open, iso)) {
            using (IO.StreamReader reader = new IO.StreamReader(isoStream)) {
                return reader.ReadToEnd;
            }
        }
    }

Tags:

Silverlight 1.1 | Tutorial

Comments

Mina , on 8/17/2008 1:20:29 AM Said:

Mina

I tried this code
try
{
System.IO.IsolatedStorage.IsolatedStorageFile iso = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
//the exception is thrown in the following statement
using (System.IO.IsolatedStorage.IsolatedStorageFileStream isoStream = new
System.IO.IsolatedStorage.IsolatedStorageFileStream("C:/Users/Valued Customer/Desktop/file.txt", System.IO.FileMode.Open, iso))
{
using (System.IO.StreamReader reader = new System.IO.StreamReader(isoStream))
{
  //this is a list to store the stream
  st.Add(reader);
}
}

                            
}
But it gave me this exception
"System.IO.IsolatedStorage.IsolatedStorageException: Operation not permitted on IsolatedStorageFileStream.\r\n  "

busby test , on 11/10/2008 1:40:08 AM Said:

busby test

Thanks for sharing this code. thanks alot

s , on 1/20/2009 6:52:51 AM Said:

s

How can I access and open the file that is uploaded in isolated storage in its own application. ...notepad, word etc

seo alaminos , on 3/24/2009 6:09:58 AM Said:

seo alaminos

now i know, a code i have to review now, thanks for the help

My Little Blog , on 4/20/2009 6:11:21 PM Said:

My Little Blog

Store and Read Files in Client's Computer Using Isolated Storage?really?what a nice post.thanks for the idea

Ronald , on 5/21/2009 2:10:09 AM Said:

Ronald

See this web app http://www.ToolToMeet.com of how isolated storage can be used instead of using a central database

Franchises for sale , on 6/9/2009 10:08:25 AM Said:

Franchises for sale

It's interesting, the blog engine platform seems very variable in form.  My design skills are not so good as my C coding though, I would be interested in seeing what additional skins you can get for it.  Nice blog btw, best wishes for it and keep up the posts. Smile  Kind regards,  Peter sims.

Ashwinkumar , on 6/21/2009 3:35:17 PM Said:

Ashwinkumar

When i am writing the content in to a isolated storage file it gives an exception operation not permitted on filestream.
sometimes it writes the content but some times it gives the exception.
please give me the solution to avoid the exception

Sulumits Retsambew , on 6/25/2009 10:20:16 AM Said:

Sulumits Retsambew

hello, this is my first time i visit here. I found so many interesting in your blog especially on how to determine the topic. keep up the good work.

computer terms , on 6/25/2009 10:21:12 AM Said:

computer terms

thanks for this nice info, it's so useful for me.

bolinaoph , on 7/6/2009 5:14:37 AM Said:

bolinaoph

Thank you so much. I've been looking for this codes. now I have it. I can continue my work.

Best Registry Cleaner Software , on 8/31/2009 1:32:36 AM Said:

Best Registry Cleaner Software

Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful

Vista Drivers , on 10/13/2009 12:46:29 PM Said:

Vista Drivers

Wow, this is very helpful. I have been trying to find a way to do this but I would have never figured it out without your help. It will make it much easier to access important form inputs.

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview
Loading