Silverlight 2.0: OpenFileDialog, et. al.

Silverlight 2.0 enables developers to open and read local files outside the Isolated Storage. I’ve tried out the examples available on the internet but most examples are made with the pre-Release To Web (RTW) version of Silverlight 2.0 and I made some examples on how to use this feature with the RTW version.

Typically, prompting the OpenFileDialog is triggered with a button click and the code snippet that you will be seeing below is a typical OpenFileDialog scenario in a Silverlight 2.0 application.


private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofdSample = new OpenFileDialog();
ofdSample.Multiselect = true;

// You can specify the file filters that you want to use.
// For example, if you want to additional files to filter
// you may use the following:
// ofdSample.Filter = "Text files (*.txt;*.xml)|*.txt;*.xml";

ofdSample.Filter = "Text Files (*.txt)|*.txt";

// The example: if (ofdSample.ShowDialog() == DialogResult.OK)
// won't work with RTW version of Silverlight 2.0

if (ofdSample.ShowDialog() == true)
{
// Suppose we enabled multi-file select (see line #4 of this snippet),
// we can get the total number of files by doing the following:

int ofdNumSelectedFiles;
ofdNumSelectedFiles = ofdSample.Files.Count(fi => fi.Name.Trim() != "");

// If in case you want to loop through the various files selected,
// a cast is needed to get the FileInfo properties of each file selected.

FileInfo[] ofdFiles;
ofdFiles = (FileInfo[])ofdSample.Files;

// An example of looping through the files.
for (int fileCounter = 0; fileCounter < ofdNumSelectedFiles; fileCounter++)
{
MessageBox.Show(ofdFiles[fileCounter].Name);
}

// Just in case you want to validate the number of
// files selected IF MultiSelect is set to true
MessageBox.Show(ofdNumSelectedFiles.ToString());

// Sometimes, we want to load text files into Silverlight
// controls such as a TextBox. Suppose I have a Silverlight
// control named "txtDescription" and I want to load the
// contents of the text file into the control, I will be
// using the following lines.

// Notice that I placed it inside a try-catch block. This
// is to ensure that there are no other processes that uses
// the file.

try
{
using (StreamReader reader = ofdSample.File.OpenText())
{
txtDescription.Text = reader.ReadToEnd();
}
}
catch (Exception ex)
{
MessageBox.Show("File is inaccessible. Error returned was: " + ex.Message, "File Access Error", MessageBoxButton.OK);
}
}
}

If you would notice on line #33, I used the “Name” property of the FileInfo class. In this part of the code, you can do manipulations such as storing the file in the database (as most implementations, we just store the filename in the database while the actual file might reside in an IsolatedStorage or somewhere in the web application where the Silverlight application is hosted).  If you are a bit lost, Visual Studio provides intellisense with the properties and methods that you want to use:

intellisense

Hope this helps! Happy Coding!

Advertisements

One thought on “Silverlight 2.0: OpenFileDialog, et. al.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s