OpenFolderDialog Class
- Supported on windows6.0.6000
Provides a modern Windows folder selection dialog using the IFileOpenDialog COM interface.
[SupportedOSPlatform("windows6.0.6000")]
public sealed class OpenFolderDialogRemarks
This dialog provides a modern Windows folder picker experience similar to the one used in File Explorer. It is only supported on Windows platforms.
Example
var dialog = new OpenFolderDialog
{
Title = "Select a folder",
InitialDirectory = @"C:\Users",
OkButtonLabel = "Select Folder",
};
if (dialog.ShowDialog() == DialogResult.OK)
{
Console.WriteLine($"Selected folder: {dialog.SelectedPath}");
}Constructors
| Name | Description |
|---|---|
| OpenFolderDialog() |
OpenFolderDialog()
public OpenFolderDialog()No documentation is available.
Properties
| Name | Description |
|---|---|
| ChangeCurrentDirectory | Gets or sets a value indicating whether to change the current working directory to the selected folder. |
| InitialDirectory | Gets or sets the initial directory to display when the dialog opens. |
| LastHResult | Gets the HRESULT returned by the last call to OpenFolderDialog.ShowDialog. |
| OkButtonLabel | Gets or sets the label text for the OK button. |
| SelectedPath | Gets the path of the folder selected by the user. This property is populated after OpenFolderDialog.ShowDialog returns DialogResult.OK. |
| Title | Gets or sets the title text displayed in the dialog's title bar. |
ChangeCurrentDirectory
OpenFolderDialog.csGets or sets a value indicating whether to change the current working directory to the selected folder.
public bool ChangeCurrentDirectory { get; set; }Property value
bool true to change the current directory to the selected folder; false to preserve the current working directory. The default is false.
InitialDirectory
OpenFolderDialog.csGets or sets the initial directory to display when the dialog opens.
public string? InitialDirectory { get; set; }Property value
Remarks
The value is resolved by the Windows shell, so it accepts a file system path as well as a shell location such as shell:Downloads. A path that uses / as a separator is normalized before being resolved. When the value cannot be resolved — it does not exist, it is malformed, or it names a drive that is not currently mounted — it is ignored and the dialog opens on the folder the shell would have chosen by default.
LastHResult
OpenFolderDialog.csGets the HRESULT returned by the last call to OpenFolderDialog.ShowDialog.
public int LastHResult { get; }Property value
Remarks
OpenFolderDialog.ShowDialog reports every failure as DialogResult.Abort, which says nothing about the cause. This property carries the underlying HRESULT so the caller can log it or turn it into an exception with Marshal.GetExceptionForHR. It is 0 until the dialog has been shown.
OkButtonLabel
OpenFolderDialog.csSelectedPath
OpenFolderDialog.csGets the path of the folder selected by the user. This property is populated after OpenFolderDialog.ShowDialog returns DialogResult.OK.
public string? SelectedPath { get; }Property value
Remarks
Each call to OpenFolderDialog.ShowDialog resets this property, so it always reflects the most recent call and is null when that call did not return DialogResult.OK.
Title
OpenFolderDialog.csMethods
| Name | Description |
|---|---|
| ShowDialog() | Shows the folder selection dialog. |
| ShowDialog(nint) | Shows the folder selection dialog with the specified owner window. |
ShowDialog overloads
ShowDialog()
OpenFolderDialog.csShows the folder selection dialog.
public DialogResult ShowDialog()Returns
DialogResult A DialogResult indicating whether the user clicked OK, Cancel, or if the operation was aborted.
ShowDialog(nint)
OpenFolderDialog.csShows the folder selection dialog with the specified owner window.
public DialogResult ShowDialog(nint owner)Parameters
ownernint- The handle to the owner window (HWND). Use
IntPtr.Zerofor no owner.
Returns
DialogResult A DialogResult indicating whether the user clicked OK, Cancel, or if the operation was aborted.