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 OpenFolderDialog

Remarks

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

NameDescription
OpenFolderDialog()

OpenFolderDialog()

public OpenFolderDialog()

No documentation is available.

Properties

NameDescription
ChangeCurrentDirectoryGets or sets a value indicating whether to change the current working directory to the selected folder.
InitialDirectoryGets or sets the initial directory to display when the dialog opens.
LastHResultGets the HRESULT returned by the last call to OpenFolderDialog.ShowDialog.
OkButtonLabelGets or sets the label text for the OK button.
SelectedPathGets the path of the folder selected by the user. This property is populated after OpenFolderDialog.ShowDialog returns DialogResult.OK.
TitleGets or sets the title text displayed in the dialog's title bar.

ChangeCurrentDirectory

OpenFolderDialog.cs

Gets 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.cs

Gets or sets the initial directory to display when the dialog opens.

public string? InitialDirectory { get; set; }

Property value

string?

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.

Gets the HRESULT returned by the last call to OpenFolderDialog.ShowDialog.

public int LastHResult { get; }

Property value

int

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

Gets or sets the label text for the OK button.

public string? OkButtonLabel { get; set; }

Property value

string?

Gets 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

string?

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.

Gets or sets the title text displayed in the dialog's title bar.

public string? Title { get; set; }

Property value

string?

Methods

NameDescription
ShowDialog()Shows the folder selection dialog.
ShowDialog(nint)Shows the folder selection dialog with the specified owner window.

ShowDialog overloads

Shows 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.cs

Shows the folder selection dialog with the specified owner window.

public DialogResult ShowDialog(nint owner)
Parameters
owner nint
The handle to the owner window (HWND). Use IntPtr.Zero for no owner.
Returns

DialogResult A DialogResult indicating whether the user clicked OK, Cancel, or if the operation was aborted.