Tutorial: Reading and Writing Files in Silverlight

Posted by Matt | Filed under , , ,

Note: This article as some inaccuracies.  Please read this follow up article for additional information: Reading Local Files in Silverlight

It's a pair of very easy questions:

How do I write to the hard drive using Silverlight?

and

How do I read from the hard drive using Silverlight?

The answer is also very easy.  You can't. [Matt: This statement is incorrect.]

However, there is an alternative, which may be good enough for many people.

Silverlight enables local storage in an isolated area.  This means that you can store files locally on the user's computer, but not in their normal file system.  Microsoft sets aside a special set of files for Silverlight applications.  It's almost like a private file system per Silverlight application.  So each application has access to it's files, and only it's files.  You cannot cross-access files between Silverlight applications, nor can you read/write to the user's normal file system. [Matt: This paragraph has some inaccuracies.]

Now, if this isn't stopping you, here's how you read and write files.

There's a namespace available to Silverlight applications called System.IO.IsolatedStorage.  This namespace has some special classes used to access files in Silverlight.

First, you need to access an IsolatedStorageFile.  This is done by calling IsolatedStorageFile.GetUserStoreForApplication() or IsolatedStorageFile.GetUserStoreForSite().  There are some differences between the functions, but either will get you a private file system in which you can read and write files.

Next, you need create or open your file almost as you would normally.  Instead of using FileStream, you would use IsolatedStorageFileStream (which is derived from FileStream).  This means that once you have your stream, you can use it anywhere you would have normally used a normal FileStream.

Next, you read or write as you normally would.

Here is an example of creating a text file and then reading from it.

// Get the storage file for the application
IsolatedStorageFile isf =
    IsolatedStorageFile.GetUserStoreForApplication();

// Open/Create the file for writing
Stream stream = new IsolatedStorageFileStrea("Silverlight.txt",
    FileMode.Create, FileAccess.Write, isf);

// Use the stream normally in a TextWriter
TextWriter writer = new StreamWriter(stream);
writer.WriteLine("Written to isolated storage");
writer.Close(); // Close the writer so data is flushed
stream.Close(); // Close the stream too

// Open the file for reading
stream = new IsolatedStorageFileStream("Silverlight.txt",
    FileMode.Open, FileAccess.Read, isf);

// Use the stream normally in a TextReader
TextReader reader = new StreamReader(stream);
string sLine = reader.ReadLine();
reader.Close(); // Close the reader
stream.Close(); // Close the stream

You can also use BinaryReader and BinaryWriter in the same way.  The only difference is the initial opening of the file.  If you want to read or write Xml data, you can use an IsolatedStorageFileStream along with part 1 and part 2 of my tutorial on Xml Serialization.

An additional note: it's possible for the user to disable local storage.  Your application should be aware of this.  You can configure Silverlight isolated storage by right clicking on a Silverlight Application and selecting "Silverlight Configuration" and clicking on the "Application Storage" tab.

image

Below are some additional references.

A follow up article has been written to address some additional information:  Reading Local Files in Silverlight

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading