Search code examples
c#windows-10openfiledialog

Using a CommonOpenFileDialog to select a folder but still show files within the folder in Windows 10


I'm trying to use CommonOpenFileDialog to allow users to select a folder, but also view the files that within the folder in the dialog. Currently my code allows the user to only select a folder, but files within it are hidden, which causes users to think they have selected a wrong (empty) folder.

using (var dlg = new CommonOpenFileDialog()
{
    Title = "Select folder to import from",
    AllowNonFileSystemItems = true,
    IsFolderPicker = true,
    EnsurePathExists = true,
    Multiselect = false,
    ShowPlacesList = true
})
{
    if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
    {
        //Do things with the selected folder and the files within it...
    }
}

How can I achieve my goal for applications targeting Windows 10?

NOTE: There is a very similar question titled "How can I make CommonOpenFileDialog select folders only, but still show files?". This question already has good answers, however, none of the solutions work in Windows 10. Because the existing question is outdated (asked over 9 years ago) and doesn't apply for Windows 10, this question is specifically asking for a solution that works on a Windows 10 device.


Solution

  • I think that is not possible with the CommonOpenFileDialog
    See: How to use IFileDialog with FOS_PICKFOLDER while still displaying file names in the dialog

    The only way is to create your own custom dialog or you can use folder dialog with P/Invoke (based on SHBrowseForFolder like FolderBrowserDialog).
    See: http://www.pinvoke.net/default.aspx/shell32.shbrowseforfolder

    Copy the class from the link above and add the option BIF_BROWSEINCLUDEFILES.

    public string SelectFolder(string caption, string initialPath, IntPtr parentHandle)
    {
        _initialPath = initialPath;
        ...
        bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_SHAREABLE | BIF_BROWSEINCLUDEFILES;
    

    Now files are displayed in the dialog unfortunately only like FolderBrowserDialog.