A typo in a link to the sample code caused the code to not appear in the topic How to: Use the Open File Dialog Box topic. Here's the C# and VB code you should have gotten with the Silverlight 2 Beta 1 SDK documentation.
C# code from the Page.xaml.cs file
using System;using System.Collections.Generic;using System.Linq;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.IO;using System.Text;
namespace SL_OpenFileDialog_CS{ public partial class Page : UserControl { public Page() { InitializeComponent(); bOpenFileDialog.MouseLeftButtonDown += new MouseButtonEventHandler(bOpenFileDialog_MouseLeftButtonDown); }
void bOpenFileDialog_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // Create open file dialog box OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Text Files (*.txt)|*.txt | All Files (*.*) | *.*"; openFileDialog1.FilterIndex = 2;
if (openFileDialog1.ShowDialog() == DialogResult.OK) { //Open the selected file to read. System.IO.Stream fileStream = openFileDialog1.SelectedFile.OpenRead();
using (StreamReader reader = new StreamReader(fileStream)) { // Read the first line from the file and write it to the text box. tbResults.Text = reader.ReadLine(); } fileStream.Close(); }
}
}}
VB code from the Page.xaml.vb file
Partial Public Class Page Inherits UserControl Public Sub New() InitializeComponent() AddHandler bOpenFileDialog.MouseLeftButtonDown, AddressOf Me.bOpenFileDialog_MouseLeftButtonDown
End Sub
Private Sub bOpenFileDialog_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs) ' Create open file dialog box Dim openFileDialog1 As OpenFileDialog = New OpenFileDialog openFileDialog1.Filter = "Text Files (*.txt)|*.txt | All Files (*.*) | *.*" openFileDialog1.FilterIndex = 1 If (openFileDialog1.ShowDialog = DialogResult.OK) Then 'Open the selected file to read. Dim fileStream As System.IO.Stream = openFileDialog1.SelectedFile.OpenRead Dim reader As System.IO.StreamReader = New System.IO.StreamReader(fileStream) ' Read the first line from the file and write it to the text box. tbResults.Text = reader.ReadLine fileStream.Close() End If
End Sub End Class