OpenFileDialog Class
Provides a dialog box that enables the user to select one or more files.
Namespace: System.Windows.Controls
Assembly: System.Windows (in System.Windows.dll)
The OpenFileDialog type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | File | Gets a FileInfo object for the selected file. If multiple files are selected, returns the first selected file. |
![]() | Files | Gets a collection of FileInfo objects for the selected files. |
![]() | Filter | Gets or sets a filter string that specifies the file types and descriptions to display in the OpenFileDialog. |
![]() | FilterIndex | Gets or sets the index of the selected item in the OpenFileDialog filter drop-down list. |
![]() | InitialDirectory | Gets or sets the directory displayed when the dialog starts. |
![]() | Multiselect | Gets or sets a value that indicates whether the OpenFileDialog allows users to select multiple files. |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ShowDialog | Displays an OpenFileDialog that is modal to the Web browser or main window. |
![]() | ShowDialog(Window) | Displays an OpenFileDialog that is modal to the specified window. |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
This class enables the user to open one or more files on a the local computer or a networked computer. The following illustration shows the OpenFileDialog in a Silverlight application running on Windows 7 and hosted in Internet Explorer.

You show the dialog box to the user by calling the ShowDialog method. For security purposes Silverlight file and print dialogs must be user-initiated. In addition, there is a limit on the time allowed between when the user initiates the dialog and when the dialog is shown. If the time limit between these actions is exceeded, an exception will occur.
You can optionally specify a filter for the dialog box by using the Filter property.
You cannot specify an initial folder for the OpenFileDialog. The first time the OpenFileDialog is displayed for an application, the initial folder is based on the user's settings. Additional displays of the OpenFileDialog for an application use the folder of the last selected file.
If you attempt to show the dialog box from KeyDown event handlers and other synchronous calls to application code, such as LayoutUpdated or SizeChanged event handlers, an exception will be thrown. An exception will not be thrown when the application is hosted in Internet Explorer, running in protected mode.
Note: |
|---|
Silverlight does not have a browse folder dialog box and you cannot use the OpenFileDialog to just select a folder. |
Note: |
|---|
The Silverlight plug-in does not support OpenFileDialog in full-screen mode. In most cases, displaying this dialog box in full-screen mode will cause the plug-in to revert to embedded mode. To avoid issues on some browsers, you should exit full-screen mode before using this class. For more information, see Full-Screen Support. |
Platform Notes
Silverlight for Windows Phone
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
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.



Note: