Freigeben über


OpenFileDialog Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Inheritance Hierarchy

System.Object
  System.Windows.Controls.OpenFileDialog

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

Syntax

'Declaration
Public NotInheritable Class OpenFileDialog
public sealed class OpenFileDialog

The OpenFileDialog type exposes the following members.

Constructors

  Name Description
Public method OpenFileDialog Initializes a new instance of the OpenFileDialog class.

Top

Properties

  Name Description
Public property File Gets a FileInfo object for the selected file. If multiple files are selected, returns the first selected file.
Public property Files Gets a collection of FileInfo objects for the selected files.
Public property Filter Gets or sets a filter string that specifies the file types and descriptions to display in the OpenFileDialog.
Public property FilterIndex Gets or sets the index of the selected item in the OpenFileDialog filter drop-down list.
Public property InitialDirectory Gets or sets the directory displayed when the dialog starts.
Public property Multiselect Gets or sets a value that indicates whether the OpenFileDialog allows users to select multiple files.

Top

Methods

  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method 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.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ShowDialog() Displays an OpenFileDialog that is modal to the Web browser or main window.
Public method ShowDialog(Window) Displays an OpenFileDialog that is modal to the specified window.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

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.

Examples

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="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="https://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="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="https://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();
            }
        }
    }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

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

Thread Safety

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