3 out of 3 rated this helpful - Rate this topic

OpenFileDialog Class

Represents a common dialog box that allows a user to specify a filename for one or more files to open.

System.Object
  Microsoft.Win32.CommonDialog
    Microsoft.Win32.FileDialog
      Microsoft.Win32.OpenFileDialog

Namespace:  Microsoft.Win32
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: Not mapped to an xmlns.
public sealed class OpenFileDialog : FileDialog
You cannot declare this managed class in XAML.

The OpenFileDialog type exposes the following members.

  Name Description
Public method OpenFileDialog Initializes a new instance of the OpenFileDialog class.
Top
  Name Description
Public property AddExtension Gets or sets a value indicating whether a file dialog automatically adds an extension to a file name if the user omits an extension. (Inherited from FileDialog.)
Public property CheckFileExists Gets or sets a value indicating whether a file dialog displays a warning if the user specifies a file name that does not exist. (Inherited from FileDialog.)
Public property CheckPathExists Gets or sets a value that specifies whether warnings are displayed if the user types invalid paths and file names. (Inherited from FileDialog.)
Public property CustomPlaces Gets or sets the list of custom places for file dialog boxes. (Inherited from FileDialog.)
Public property DefaultExt Gets or sets a value that specifies the default extension string to use to filter the list of files that are displayed. (Inherited from FileDialog.)
Public property DereferenceLinks Gets or sets a value indicating whether a file dialog returns either the location of the file referenced by a shortcut or the location of the shortcut file (.lnk). (Inherited from FileDialog.)
Public property FileName Gets or sets a string containing the full path of the file selected in a file dialog. (Inherited from FileDialog.)
Public property FileNames Gets an array that contains one file name for each selected file. (Inherited from FileDialog.)
Public property Filter Gets or sets the filter string that determines what types of files are displayed from either the OpenFileDialog or SaveFileDialog. (Inherited from FileDialog.)
Public property FilterIndex Gets or sets the index of the filter currently selected in a file dialog. (Inherited from FileDialog.)
Public property InitialDirectory Gets or sets the initial directory that is displayed by a file dialog. (Inherited from FileDialog.)
Public property Multiselect Gets or sets an option indicating whether OpenFileDialog allows users to select multiple files.
Protected property Options Gets the Win32 common file dialog flags that are used by file dialogs for initialization. (Inherited from FileDialog.)
Public property ReadOnlyChecked Gets or sets a value indicating whether the read-only check box displayed by OpenFileDialog is selected.
Public property RestoreDirectory This property is not implemented. (Inherited from FileDialog.)
Public property SafeFileName Gets a string that only contains the file name for the selected file. (Inherited from FileDialog.)
Public property SafeFileNames Gets an array that contains one safe file name for each selected file. (Inherited from FileDialog.)
Public property ShowReadOnly Gets or sets a value indicating whether OpenFileDialog contains a read-only check box.
Public property Tag Gets or sets an object associated with the dialog. This provides the ability to attach an arbitrary object to the dialog. (Inherited from CommonDialog.)
Public property Title Gets or sets the text that appears in the title bar of a file dialog. (Inherited from FileDialog.)
Public property ValidateNames Gets or sets a value indicating whether the dialog accepts only valid Win32 file names. (Inherited from FileDialog.)
Top
  Name Description
Protected method CheckPermissionsToShowDialog Determines whether sufficient permissions for displaying a dialog exist. (Inherited from CommonDialog.)
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 it 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 HookProc Defines the common file dialog hook procedure that is overridden to add common functionality to a file dialog. (Inherited from FileDialog.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method OnFileOk Raises the FileOk event. (Inherited from FileDialog.)
Public method OpenFile Opens a read-only stream for the file that is selected by the user using OpenFileDialog.
Public method OpenFiles Creates an array that contains one read-only stream for each file selected by the user using OpenFileDialog.
Public method Reset Resets all OpenFileDialog properties to their default values. (Overrides FileDialog.Reset().)
Protected method RunDialog RunDialog is called to display a file dialog in a derived class, such as OpenFileDialog and SaveFileDialog. (Inherited from FileDialog.)
Public method ShowDialog() Displays a common dialog. (Inherited from CommonDialog.)
Public method ShowDialog(Window) Displays a common dialog. (Inherited from CommonDialog.)
Public method ToString Returns a string that represents a file dialog. (Inherited from FileDialog.)
Top
  Name Description
Public event FileOk Occurs when the user selects a file name by either clicking the Open button of the OpenFileDialog or the Save button of the SaveFileDialog. (Inherited from FileDialog.)
Top

The following figure shows an OpenFileDialog for Windows Vista.

Open dialog box

Starting in Windows Vista, open and save file dialog boxes have a Favorite Links panel on the left side of the dialog box that allows the user to quickly navigate to a different location. These links are called custom places. Use the CustomPlaces property to set this list of links.

The following example shows how to create an OpenFileDialog that contains a default file name and extension type.


// Configure open file dialog box
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".txt"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension

// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();

// Process open file dialog box results
if (result == true)
{
    // Open document
    string filename = dlg.FileName;
}


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Figure
The screenshot represent the Windows 7 version of the open file dialog and not the Windows Vista one.
Difference compared to System.Windows.Forms.OpenFileDialog
When running Windows 7, the System.Windows.Forms.OpenFileDialog shows the Windows 7 version of the open file dialog. Microsoft.Win32.OpenFileDialog does not - it shows the dialog pictured above.

This is tested with Visual Studio 2008 SP1 and .NET Framework 3.5 SP1.