Tutorial: Reading Local Files in Silverlight

Posted by Matt | Filed under , ,

Sample Project: SilverlightFileAccess.zip (489.06 kb)

In a previous article, I mentioned that reading and writing to the user's local file system was not allowed.  This wasn't 100% true. 

In reality, you can read a file from the user's local file system as long as you present a "File Open" dialog to the user.  The user can then select the file to open and your Silverlight application can open the selected files and only those files.

This is done for security reasons.  With today's shady characters on the internet writing viruses and phishing tools, Silverlight was built with security in mind.  This means (a) that it's more secure for the end user, and (b) as developers, we're limited in what we can do.  So a Silverlight application cannot open a file for reading unless the user specifically selected that file for the purposes of being read.

Here is some Xaml for my test application.

<Grid x:Name="LayoutRoot" Background="Black">
    <Button Content="Open..." 
            Width="75" 
            Height="25" 
            Click="Button_Click" 
            VerticalAlignment="Top"
            HorizontalAlignment="Left"/>
    <TextBlock x:Name="Status" 
               Margin="0,25,0,0" 
               Foreground="White"/>
		
    <Image x:Name="Image" 
           MinWidth="100" 
           MinHeight="100" 
           Margin="0,50,0,0"/>
</Grid>

In order to read a file, you need to create a FileOpenDialog object and present that to the user.

OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.Filter = "JPEG Files (*.jpg)|*.jpg";
bool bResult = (bool)dlg.ShowDialog();
if (!bResult)
    return;

Here, I am showing the dialog to the user and asking for a JPEG file.  I am also limiting the open to a single file.  You can have the user select multiple files as well as provide more choices to the user as to the type of file to open.

Once the user has selected the file, you access the file information from the OpenFileDialog.SelectedFile member.  This member is of type FileDialogFileInfo.  From this class, you can get the name of the file (but not the full path) as well as open a Stream to the file.  The stream is a standard System.IO.Stream class which you can read normally.

Here, I am opening the stream, but then funnelling it into an Image object to be displayed on my form.

FileDialogFileInfo info = dlg.SelectedFile;
Status.Text = info.Name;
Stream s = info.OpenRead();

BitmapImage bi = new BitmapImage();
bi.SetSource(s);
Image.Source = bi;
s.Close();

In this example, once I open the file, the selected image should appear on the page.

However, your application can read from the file however it wants:  as a text file, Xml, or other binary format.

But what about writing to a file.  Unfortunately, at this time, there is not an equivalent SaveFileDailog class.  However, there is a workaround.  To Save a file, you need to trigger a web browser file download (ie. a normal file download via the web browser).  So data would need to be sent to the server, and your hosting web application would need to trigger the download.  This will be the topic for a future article.

Comments

August 18, 2008 21:10

trackback

Trackback from DotNetKicks.com

Tutorial: Reading Local Files in Silverlight

DotNetKicks.com

November 15, 2008 09:25

Andrej Juhas

FileDialogFileInfo don't work because it is System.Windows.Controls .

Andrej Juhas Slovakia

November 15, 2008 10:13

matt

This article was written during Beta 2 of Silverlight.  Some things have changed including what you mention.  Now it's just FileInfo instead of FileDialogFileInfo.  The member property is also just File instead of SelectedFile.

matt Canada

November 19, 2008 07:48

Andrej Juhas

Ok . I was already in depression Smile . It is'not there, in final version Smile

Andrej Juhas Slovakia

November 19, 2008 08:02

Andrej Juhas

Silverlight 2 not beta version:

FileInfo info = dlg.File ;

Andrej Juhas Slovakia

December 4, 2008 05:36

vardis

Are there any other changes fro mthe beta versions?

vardis United Kingdom

December 4, 2008 05:43

Matt

This was basically the only change I needed to do to get my sample program and photo gallery working.  I am not aware of any other changes.

Matt Canada

August 28, 2009 17:31

mmg

In reality, you can read a file from the user's local file system as long as you present a "File Open" dialog to the user.  The user can then select the file to open and your Silverlight application can open the selected files and only those files.
which is not entirely true:
because you can read files by creating a soap web client. without an open file dialog.

mmg France

Comments are closed