Reading Local Files with OpenFileDialog
Although Silverlight applications aren't allowed to access the file system directly, there's one backdoor you can use to read a file that's on the local file system. That backdoor is the OpenFileDialog class.
The OpenFileDialog allows you to show the ordinary Open File dialog box. Once the user chooses a file, it's then made available to your application for reading only. No restrictions are placed on the OpenFileDialog, so it's possible for the user to choose any file. However, there's no way for you to access any file without the user explicitly choosing it and clicking Open, which is considered to be a high enough bar for security.
To use the OpenFileDialog, you first create a new instance and then set the Filter and Filterlndex properties to configure what file types the user will see. The Filter determines what appears in the file type list.
You need to indicate the text that should appear in the file type list, and the corresponding expression that the OpenFileDialog box will use to filter files. For example, if you want to allow the user to open text files, you might show the text "Text Files (*.txt)" and use the filter expression *.txt to find all files with the .txt extension. Here's how you would then set the Filter property:
OpenFileDialog dialog = new OpenFileDialo (); dialog.Filter = "Text Files (*.txt)|*.txt";
You use the | (pipe) character to separate the display text from the filter expression in the filter string. If you have multiple file types, you string them one after the other, separated by additional pipe characters. For example, if you want to allow the user to see different types of images, you might write the filter string like this:
dialog.Filter = "Bitmaps (*.bmp)|*.bmp|JPEGs (*.jpg)|*.jpg|All files (*.*)|*.*";
You can also create a filter expression that matches several file types, by separating them with semicolons:
dialog.Filter = "Image Files(*.bmp;*.jpg;*.gif)|*.bmp;*.jpg;*.gif";
Once you've configured the OpenFileDialog, you then show the dialog box by calling ShowDialog(). The ShowDialog() method returns a DialogResult value that indicates what the user selected. If the result is true, the user picked a file and you can go ahead and open it.
The file is exposed through the OpenFileDialog.File property, which is a FileDialogFileInfo object. The FileDialogFilelnfo is a relatively simply class that exposes just three useful members: a Name property that returns the file name, an OpenRead() method that returns a FileStream in read-only mode, and an OpenText() method that creates the FileStream and returns a StreamReader for it.
using (itreamReader reader = dlg.File.OpenText()) {
string data = reader.ReadToEnd();
Obviously, the OpenText() method is a good shortcut if you're dealing with text data, and the OpenRead() method is a better choice if you need to create a BinaryReader or use the FileStream.ReadQ method directly to pull out a block of bytes.
Tip The OpenFileDialog also supports multiple selection. Simply set OpenFileDialog.Multiselect to true before you call ShowDialog(). Then, retrieve all the selected files through the OpenFileDialog.Files property.
One interesting way to use the OpenFileDialog is to copy a selected file from the local hard drive to isolated storage, so it can be manipulated by the application later on. Here's an example that performs this trick:
OpenFileDialog dialog = new OpenFileDialc (); dialog.Filter = "All files (*.*)|*.*"; dialog.Multiselect = true;
// Copy all the selected files to isolated storage. using (IsolatedStorageFile store =
IsolatedStorageFile.GetUserStoreForApplication())
foreach ( ileDialogFilelnfo file in dialog.Files) {
using ( tream fileStream = file.OpenRead()) {
// Check for free space.
if (fileStream.Length > store.AvailableFreeSpace) {
// (Cancel the operation or use IncreaseOuotaTo().)
using (IsolatedStorageFileStream storeStream = store.CreateFile(file.Name))
// Write 1 KB block at a time. byte[] buffer = new byte[l024]; int count = 0;
int count = fileStream.Read(buffer, 0, buffer.Length); if (count > 0) storeStream.Write(buffer, 0, count); } while (count > 0);
Post a comment