Export (0) Print
Expand All

OpenFileDialog Class

Silverlight

Provides a dialog box that enables the user to select one or more files.

System.Object
  System.Windows.Controls.OpenFileDialog

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

public sealed class OpenFileDialog

The OpenFileDialog type exposes the following members.

  NameDescription
Public methodOpenFileDialogInitializes a new instance of the OpenFileDialog class.
Top

  NameDescription
Public propertyFileGets a FileInfo object for the selected file. If multiple files are selected, returns the first selected file.
Public propertyFilesGets a collection of FileInfo objects for the selected files.
Public propertyFilterGets or sets a filter string that specifies the file types and descriptions to display in the OpenFileDialog.
Public propertyFilterIndexGets or sets the index of the selected item in the OpenFileDialog filter drop-down list.
Public propertyInitialDirectoryGets or sets the directory displayed when the dialog starts.
Public propertyMultiselectGets or sets a value that indicates whether the OpenFileDialog allows users to select multiple files.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodShowDialog()Displays an OpenFileDialog that is modal to the Web browser or main window.
Public methodShowDialog(Window)Displays an OpenFileDialog that is modal to the specified window.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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.

Open File Dialog

Open file dialog box.

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.

NoteNote:

Silverlight does not have a browse folder dialog box and you cannot use the OpenFileDialog to just select a folder.

NoteNote:

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 Silverlight for Windows Phone

 OpenFileDialog is not supported in 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>



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();
            }
        }
    }
}


Silverlight

Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft