PickFolders option for OpenFileDialog (minimal) by miloush · Pull Request #6374 · dotnet/wpf (original) (raw)

For discussion. Fixes #438. Further discussion in #4039. Previous attempt: #6351

Description

Allows developers to ask the user to select a folder, a feature known as FolderBrowserDialog in WinForms.

This PR is the minimal code changes needed to introduce the feature. It closely reflects the underlying API design, i.e. the ability to specify FOS_PICKFOLDERS on the IOpenFileDialog. This is a backwards compatible solution - no public API has been moved or removed and no existing behavior has changed.

A custom option OPTION_PICKFOLDERS is introduced that sets FOS_PICKFOLDERS and clears OFN_FILEMUSTEXIST when the dialog is prepared.

New visible members

In OpenFileDialog:

Legacy behavior

Legacy code path (pre-Vista) ignores the flag and shows standard file open dialog.

Customer Impact

Without this fix, users have to either use WinForms' FolderBrowserDialog, resort to 3rd party libraries or re-implement the whole IFileDialog API. #438 is currently the top voted issue in the repository.

Regression

No.

Testing

Compiled custom PresentationFramework.dll and tested in 6.0.2 x86 app that both folder browsing and file browsing works as expected.

Risk

Low - existing behavior not affected, minimal code changes. Setting Filter on a dialog with PickFolders will throw COMException on ShowDialog().

The bit used by OPTION_PICKFOLDERS is currently unused in both legacy and IOpenFileDialog API, but it might get allocated in the future. This would however not break this feature as the bits are masked out (same happened in the past with OPTION_ADDEXTENSION).

User code that processes "used" file dialogs could now be handed an instance that unexpectedly contains folders in the FileName(s) properties. This, however, would have to be explicitly caused using new code. Unexpected file operations on folders (i.e. trying to open them) yield IO exceptions that the existing code should be already handling.

/cc @ThomasGoulet73 @batzen

Microsoft Reviewers: Open in CodeFlow