How to: Use the Open File Dialog Box

You can enable users to select files in a Silverlight-based application by using the OpenFileDialog class. After the user has selected one or more files, you can process the selected files.

NoteNote:

If a selected file is sent to the server for processing, you should inform the user.

To display the open file dialog box and process user input

  1. Create an instance of the OpenFileDialog class.

  2. Optionally, specify the file filter string and the filter index by setting the Filter and FilterIndex properties.

  3. Call the ShowDialog method to display the open file dialog box.

  4. When the method returns, test whether the dialog box was closed with the OK button or the Cancel button. If it was closed with the OK button, process the user's selection.

The following example shows how to display the dialog box, test the user's selection, and then process the file.

Example

<UserControl x:Class="SL_OpenFileDialog_CS.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Button x:Name="bOpenFileDialog" Content="Open File"
                 Height="30" Width="60" Margin="10"
                 HorizontalAlignment="Left" VerticalAlignment="Top" 
                 Click="bOpenFileDialog_Click" />

        <TextBox x:Name="tbResults" Text="Silverlight Results"
                 Height="30" Width="300" Margin="10,50"
                 HorizontalAlignment="Left" VerticalAlignment="Top" 
                 Background="Beige" />
    </Grid>
</UserControl>
<UserControl x:Class="SL_OpenFileDialog_VB.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Button x:Name="bOpenFileDialog" Content="Open File"
                 Height="30" Width="60" Margin="10"
                 HorizontalAlignment="Left" VerticalAlignment="Top" 
                 Click="bOpenFileDialog_Click" />

        <TextBox x:Name="tbResults" Text="Silverlight Results"
                 Height="30" Width="300" Margin="10,50"
                 HorizontalAlignment="Left" VerticalAlignment="Top" 
                 Background="Beige" />
    </Grid>
</UserControl>
Public Sub New()
    InitializeComponent()
End Sub

Private Sub bOpenFileDialog_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    ' Create an instance of the open file dialog box.
    Dim openFileDialog1 As OpenFileDialog = New OpenFileDialog

    ' Set filter options and filter index.
    openFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
    openFileDialog1.FilterIndex = 1

    openFileDialog1.Multiselect = True

    ' Call the ShowDialog method to show the dialogbox.
    dim UserClickedOK as Boolean = openFileDialog1.ShowDialog

    ' Process input if the user clicked OK.
    If (UserClickedOK = True) Then
        'Open the selected file to read.
        Dim fileStream As System.IO.Stream = openFileDialog1.File.OpenRead

        Using reader As New System.IO.StreamReader(fileStream)
            ' Read the first line from the file and write it to the text box.
            tbResults.Text = reader.ReadLine
        End Using
        fileStream.Close()
    End If
End Sub
using System.Windows;
using System.Windows.Controls;

namespace SL_OpenFileDialog_CS
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }

        private void bOpenFileDialog_Click(object sender, RoutedEventArgs e)
        {
            // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            // Set filter options and filter index.
            openFileDialog1.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;

            openFileDialog1.Multiselect = true;

            // Call the ShowDialog method to show the dialog box.
            bool? userClickedOK = openFileDialog1.ShowDialog();

            // Process input if the user clicked OK.
            if (userClickedOK == true)
            {
                // Open the selected file to read.
                System.IO.Stream fileStream = openFileDialog1.File.OpenRead();

                using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
                {
                    // Read the first line from the file and write it the textbox.
                    tbResults.Text = reader.ReadLine();
                }
                fileStream.Close();
            }
        }
    }
}

See Also

Reference

Other Resources