Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new OpenFolderDialog API article #1715

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Windows implements different kinds of reusable dialog boxes that are common to a

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.

WPF encapsulates the open file, save file, and print common dialog boxes and exposes them as managed classes for you to use in standalone applications.
WPF encapsulates the open file, save file, open folder, and print common dialog boxes and exposes them as managed classes for you to use.

:::image type="content" source="media/dialog-boxes-overview/open-file-dialog-box.png" alt-text="Open file dialog box called from WPF.":::

Expand All @@ -52,6 +52,7 @@ To learn more about common dialog boxes, see the following articles:
- [How to display a common dialog box](how-to-open-common-system-dialog-box.md)
- [Show the Open File dialog box](how-to-open-common-system-dialog-box.md#open-file-dialog-box)
- [Show the Save File dialog box](how-to-open-common-system-dialog-box.md#save-file-dialog-box)
- [Open Folder dialog box](how-to-open-common-system-dialog-box.md#open-folder-dialog-box)
- [Show the Print dialog box](how-to-open-common-system-dialog-box.md#print-dialog-box)

## Custom dialog boxes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The open file dialog box is used by file opening functionality to retrieve the n

:::image type="content" source="media/how-to-open-common-system-dialog-box/open-file-dialog-box.png" alt-text="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 <xref:Microsoft.Win32.OpenFileDialog> class and is located in the <xref:Microsoft.Win32> namespace. The following code shows how to create, configure, and show one, and how to process the result.
The common open file dialog box is implemented as the <xref:Microsoft.Win32.OpenFileDialog> class and is located in the <xref:Microsoft.Win32> namespace. The following code shows how to create, configure, and show the dialog.

:::code language="csharp" source="snippets/how-to-open-common-system-dialog-box/csharp/Window1.xaml.cs" id="OpenFile":::
:::code language="vb" source="snippets/how-to-open-common-system-dialog-box/vb/Window1.xaml.vb" id="OpenFile":::
Expand All @@ -38,18 +38,34 @@ The save file dialog box is used by file saving functionality to retrieve the na

:::image type="content" source="media/how-to-open-common-system-dialog-box/save-file-dialog-box.png" alt-text="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 <xref:Microsoft.Win32.SaveFileDialog> class, and is located in the <xref:Microsoft.Win32> namespace. The following code shows how to create, configure, and show one, and how to process the result.
The common save file dialog box is implemented as the <xref:Microsoft.Win32.SaveFileDialog> class, and is located in the <xref:Microsoft.Win32> namespace. The following code shows how to create, configure, and show the dialog.

:::code language="csharp" source="snippets/how-to-open-common-system-dialog-box/csharp/Window1.xaml.cs" id="SaveFile":::
:::code language="vb" source="snippets/how-to-open-common-system-dialog-box/vb/Window1.xaml.vb" id="SaveFile":::

For more information on the save file dialog box, see <xref:Microsoft.Win32.SaveFileDialog?displayProperty=nameWithType>.

## 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.

:::image type="content" source="media/how-to-open-common-system-dialog-box/open-folder-dialog-box.png" alt-text="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 <xref:Microsoft.Win32.OpenFolderDialog> class, and is located in the <xref:Microsoft.Win32> namespace. The following code shows how to create, configure, and show the dialog.

:::code language="csharp" source="snippets/how-to-open-common-system-dialog-box/csharp/Window1.xaml.cs" id="OpenFolder":::
:::code language="vb" source="snippets/how-to-open-common-system-dialog-box/vb/Window1.xaml.vb" id="OpenFolder":::

For more information on the open folder dialog box, see <xref:Microsoft.Win32.OpenFolderDialog?displayProperty=nameWithType>.

## Print dialog box

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

:::image type="content" source="media/how-to-open-common-system-dialog-box/print-data-dialog-box.png" alt-text="A print dialog box shown from a WPF application.":::
:::image type="content" source="media/how-to-open-common-system-dialog-box/print-data-dialog-box-win11.png" alt-text="A print dialog box shown from a WPF application.":::

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

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
<ApplicationManifest>app.manifest</ApplicationManifest>

</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<Button Click="Button_Click">Message Box</Button>
<Button Click="Save_Click">Save file</Button>
<Button Click="Open_Click">Open file</Button>
<Button Click="OpenFolder_Click">Open folder</Button>
<Button Click="Print_Click">Print</Button>
</StackPanel>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,54 @@ private void Open_Click(object sender, RoutedEventArgs e)
//</OpenFile>
}

private void OpenFolder_Click(object sender, RoutedEventArgs e)
{
//<OpenFolder>
// 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;
}
//</OpenFolder>
}

private void OpenFolderMultiple_Click(object sender, RoutedEventArgs e)
{
//<OpenFolderMultiselect>
// Configure open folder dialog box
Microsoft.Win32.OpenFolderDialog dialog = new();

dialog.Multiselect = true;
dialog.Title = "Select one or more folders";

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

// Process open folder dialog box results
if (result == true)
{
// Get multiple folder names
for (int index = 0; index < dialog.FolderNames.Length; index++)
{
// Get the selected folder
string fullPathToFolder = dialog.FolderNames[index];
string folderNameOnly = dialog.SafeFolderNames[index];
}
}
//</OpenFolderMultiselect>
}

private void Print_Click(object sender, RoutedEventArgs e)
{
//<Print>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<RootNamespace>WindowsOverview</RootNamespace>
<UseWPF>true</UseWPF>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<Button Click="Button_Click">Message Box</Button>
<Button Click="Save_Click">Save file</Button>
<Button Click="Open_Click">Open file</Button>
<Button Click="OpenFolder_Click">Open folder</Button>
<Button Click="Print_Click">Print</Button>
</StackPanel>
</Window>
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,51 @@
'</OpenFile>
End Sub

Private Sub OpenFolder_Click(sender As Object, e As RoutedEventArgs)
'<OpenFolder>
' Configure open folder dialog box
Dim dialog As New Microsoft.Win32.OpenFolderDialog()

dialog.Multiselect = False
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 the selected folder
Dim fullPathToFolder As String = dialog.FolderName
Dim folderNameOnly As String = dialog.SafeFolderName
End If
'</OpenFolder>
End Sub

Private Sub OpenFolderMultiple_Click(sender As Object, e As RoutedEventArgs)
'<OpenFolder>
' 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
'</OpenFolder>
End Sub

Private Sub Print_Click(sender As Object, e As RoutedEventArgs)
'<Print>
' Configure printer dialog box
Expand Down
Loading