Silverlight
2.0 Alpha introduced OpenFileDialog control which allows Silverlight
applications to open and read *local* files outside Isolated Storage. It's better than the HTML <input> tag which is widely used for uploading files in web page. Well, like
HTML <input>, OpenFileDialog could be used to build a file uploader
quickly. Both must prompt user to select files. But unlike HTML <input>,
OpenFileDialog control could be used to read and process the file data locally
in Silverlight application. This is pretty powerful since Silverlight
application will not need to upload the files to server for processing. What's more is that OpenFileDialog allows user to select multiple files at once. No need to create multiple instances of OpenFileDialog for that. Let's see some examples:
VB.NET Example 1: Open and read a single text file
Dim dlg As New Controls.OpenFileDialog
dlg.Filter = "Text Files (*.txt)|*.txt"
If dlg.ShowDialog = DialogResult.OK Then
Using reader As IO.StreamReader = dlg.SelectedFile.OpenText
'Store file content in 'text' variable
Dim text As String = reader.ReadToEnd
End Using
End If
C# Example 1: Open and read a single text file
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Text Files (*.txt)|*.txt";
if (dlg.ShowDialog() == DialogResult.OK)
{
using (StreamReader reader = dlg.SelectedFile.OpenText())
// Store file content in 'text' variable
string text = reader.ReadToEnd();
}
}
VB.NET Examples 2: Copy files to the application's isolated storage.
Imports System.IO.IsolatedStorage
...
Dim dlg As New Controls.OpenFileDialog
dlg.Filter = "All files (*.*)|*.*"
dlg.EnableMultipleSelection = True
If dlg.ShowDialog = DialogResult.OK Then
' Save all selected files into application's isolated storage
Dim iso As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication
For Each file As FileDialogFileInfo In dlg.SelectedFiles
Using fileStream As IO.Stream = file.OpenRead
Using isoStream As New _
IsolatedStorageFileStream(file.Name, IO.FileMode.Create, iso)
' Read and write the data block by block until finish
Do
Dim buffer(100000) As Byte
Dim count As Integer = fileStream.Read(buffer, 0, buffer.Length)
If count > 0 Then
isoStream.Write(buffer, 0, count)
Else
Exit Do
End If
Loop
End Using
End Using
Next
End If
C# Example 2: Copy files to the application's isolated storage.
using System.Windows.Controls;
using System.IO;
using System.IO.IsolatedStorage;
...
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "All files (*.*)|*.*";
dlg.EnableMultipleSelection = true;
if (dlg.ShowDialog() == DialogResult.OK) {
// Save all selected files into application's isolated storage
IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
foreach (FileDialogFileInfo file in dlg.SelectedFiles) {
using (Stream fileStream = file.OpenRead()) {
using (IsolatedStorageFileStream isoStream =
new IsolatedStorageFileStream(file.Name, FileMode.Create, iso)) {
// Read and write the data block by block until finish
while(true) {
byte[] buffer = new byte[100001];
int count = fileStream.Read(buffer, 0, buffer.Length);
if (count > 0) {
isoStream.Write(buffer, 0, count);
}
else {
break;
}
}
}
}
}
}