Upraviť

Zdieľať cez


How to open a common dialog box (WPF .NET)

This article demonstrates how you can display a common system dialog box in Windows Presentation Foundation (WPF). Windows implements different kinds of reusable dialog boxes that are common to all applications, including dialog boxes for selecting files and printing.

Since these dialog boxes are provided by the operating system, they're shared among all the applications that run on the operating system. These dialog boxes provide a consistent user experience, and are known as common dialog boxes. As a user uses a common dialog box in one application, they don't need to learn how to use that dialog box in other applications.

A message box is another common dialog box. For more information, see How to open a message box.

Open File dialog box

The open file dialog box is used by file opening functionality to retrieve the name of a file to open.

An Open dialog box showing the location to retrieve the file shown from a WPF application.

The common open file dialog box is implemented as the OpenFileDialog class and is located in the Microsoft.Win32 namespace. The following code shows how to create, configure, and show the dialog.

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

// Show open file dialog box
bool? result = dialog.ShowDialog();

// Process open file dialog box results
if (result == true)
{
    // Open document
    string filename = dialog.FileName;
}
' Configure open file dialog box
Dim dialog As New Microsoft.Win32.OpenFileDialog()
dialog.FileName = "Document" ' Default file name
dialog.DefaultExt = ".txt" ' Default file extension
dialog.Filter = "Text documents (.txt)|*.txt" ' Filter files by extension

' Show open file dialog box
Dim result As Boolean? = dialog.ShowDialog()

' Process open file dialog box results
If result = True Then
    ' Open document
    Dim filename As String = dialog.FileName
End If

For more information on the open file dialog box, see Microsoft.Win32.OpenFileDialog.

Save File dialog box

The save file dialog box is used by file saving functionality to retrieve the name of a file to save.

A Save As dialog box showing the location to save the file shown from a WPF application.

The common save file dialog box is implemented as the SaveFileDialog class, and is located in the Microsoft.Win32 namespace. The following code shows how to create, configure, and show the dialog.

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

// Show save file dialog box
bool? result = dialog.ShowDialog();

// Process save file dialog box results
if (result == true)
{
    // Save document
    string filename = dialog.FileName;
}
' Configure save file dialog box
Dim dialog As New Microsoft.Win32.SaveFileDialog()
dialog.FileName = "Document" ' Default file name
dialog.DefaultExt = ".txt" ' Default file extension
dialog.Filter = "Text documents (.txt)|*.txt" ' Filter files by extension

' Show save file dialog box
Dim result As Boolean? = dialog.ShowDialog()

' Process save file dialog box results
If result = True Then
    ' Save document
    Dim filename As String = dialog.FileName
End If

For more information on the save file dialog box, see Microsoft.Win32.SaveFileDialog.

Open Folder dialog box

Important

The Open Folder dialog box is available in .NET 8.0 and later.

The Open Folder dialog box is used by the user to select one or more folders, and return them to the program. For example, if your program displayed information about a folder, such as the amount of files and the file names in the folder, you can use the Open Folder dialog to let the user choose the folder.

An Open Folder dialog box showing the Pictures folder with the Camera Roll folder selected, shown from a WPF application.

The common open folder dialog box is implemented as the OpenFolderDialog class, and is located in the Microsoft.Win32 namespace. The following code shows how to create, configure, and show the dialog.

// Configure open folder dialog box
Microsoft.Win32.OpenFolderDialog dialog = new();

dialog.Multiselect = false;
dialog.Title = "Select a folder";

// Show open folder dialog box
bool? result = dialog.ShowDialog();

// Process open folder dialog box results
if (result == true)
{
    // Get the selected folder
    string fullPathToFolder = dialog.FolderName;
    string folderNameOnly = dialog.SafeFolderName;
}
' Configure open folder dialog box
Dim dialog As New Microsoft.Win32.OpenFolderDialog()

dialog.Multiselect = True
dialog.Title = "Select a folder"

' Show open folder dialog box
Dim result As Boolean? = dialog.ShowDialog()

' Process open folder dialog box results
If result = True Then

    ' Get multiple folder names
    For index = 0 To dialog.FolderNames.Length
        ' Get the selected folder
        Dim fullPathToFolder As String = dialog.FolderNames(index)
        Dim folderNameOnly As String = dialog.SafeFolderNames(index)
    Next

End If

For more information on the open folder dialog box, see Microsoft.Win32.OpenFolderDialog.

The print dialog box is used by printing functionality to choose and configure the printer that a user wants to print data to.

A print dialog box shown from a WPF application.

The common print dialog box is implemented as the PrintDialog class, and is located in the System.Windows.Controls namespace. The following code shows how to create, configure, and show one.

// Configure printer dialog box
var dialog = new System.Windows.Controls.PrintDialog();
dialog.PageRangeSelection = System.Windows.Controls.PageRangeSelection.AllPages;
dialog.UserPageRangeEnabled = true;

// Show save file dialog box
bool? result = dialog.ShowDialog();

// Process save file dialog box results
if (result == true)
{
    // Document was printed
}
' Configure printer dialog box
Dim dialog As New System.Windows.Controls.PrintDialog()
dialog.PageRangeSelection = System.Windows.Controls.PageRangeSelection.AllPages
dialog.UserPageRangeEnabled = True

' Show save file dialog box
Dim result As Boolean? = dialog.ShowDialog()

' Process save file dialog box results
If result = True Then
    ' Document was printed
End If

For more information on the print dialog box, see System.Windows.Controls.PrintDialog. For detailed discussion of printing in WPF, see Printing overview.

See also