Export (0) Print
Expand All

How to: Use the Open File Dialog Box

Silverlight

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.

Note Note:

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.


<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


Community Additions

ADD
Show:
© 2014 Microsoft