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

Comments

August 9, 2008 11:39

trackback

Trackback from DotNetKicks.com

Tutorial: Reading and Writing Files in Silverlight

DotNetKicks.com

August 15, 2008 05:27

unruledboy

"You cannot cross-access files between Silverlight applications"

IsolatedStorageFile.GetUserStoreForSite is for apps in the same site, right?

unruledboy People's Republic of China

August 15, 2008 10:53

Matt

You are correct.  You can share data between 2 silverlight applications on the same site.  IsolatedStorageFile.GetUserStoreForSite() sets up a user store for the site.  Most likely differentiated by the host and port.

Having 2 Silverlight apps on the same page can pass files between the apps.

Having 2 apps on different pages on the same site/port can pass files.

Having 2 pages on the same site, but different port cannot pass files.

...Matt

Matt Canada

August 17, 2008 09:11

matt

You are also able to access files on the local file system if you go through a FileOpenDialog first.  So you can show a FileOpenDialog to the user, the user can select files, and then you can access those files.

I'll make a new post on this later this week.

matt Canada

February 16, 2010 23:57

pingback

Pingback from yippeesoft.com

YippeeSoft开心软件  » Blog Archiv   » Silverlight OpenFileDialog

yippeesoft.com

Comments are closed