17

I'm trying to use IFileDialog to select a folder and the following code does this just fine. The problem is I'd like to see certain file types as well as folders while browsing (such as *.txt). Is there a simple way to do this?

//g_path is a global which will contain the selected folders path
void PickContainer()
{
    IFileDialog *pfd;
    if (SUCCEEDED(CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd))))
    {
        DWORD dwOptions;
        if (SUCCEEDED(pfd->GetOptions(&dwOptions)))
        {
            pfd->SetOptions(dwOptions | FOS_PICKFOLDERS);
        }
        if (SUCCEEDED(pfd->Show(NULL)))
        {
            IShellItem *psi;
            if (SUCCEEDED(pfd->GetResult(&psi)))
            {
                if(!SUCCEEDED(psi->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &g_path)))
                {
                    MessageBox(NULL, "GetIDListName() failed", NULL, NULL);
                }
                psi->Release();
            }
        }
        pfd->Release();
    }
}
Grizz
  • 603
  • 2
  • 7
  • 11
  • Note that if you use the code above, when you are done with the path contained in `g_path` it should be released by using `CoTaskMemFree()`. See [IShellItem::GetDisplayName()](https://learn.microsoft.com/en-us/windows/desktop/api/shobjidl_core/nf-shobjidl_core-ishellitem-getdisplayname) for details. – dgnuff Sep 19 '18 at 20:49
  • 1
    Thank you very much for your snippet! While this wasn't my problem, the snippet works flawlessly for me as well. – Pythagoras of Samos Oct 30 '18 at 15:05

2 Answers2

10

Once you opt for FOS_PICKFOLDERS then you can't see files in the dialog, only folders. If you omit FOS_PICKFOLDERS then you can't select folders, only files. The standard dialog does not support what you are asking. You could write you own dialog but I'd be inclined to find a way to organise your application to fit around the behaviour of the standard dialog.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 8
    It's not really about organizing the application better. It's that the standard dialog still isn't best for users (though it's a heck of a lot better than the pre-Vista option)—I find it confusing to have it appear that all my folders are empty while I'm browsing around, even if my ultimate target happens to be a folder rather than a file. – Owen Dec 15 '11 at 18:58
  • 4
    In my case, I'm interested in adding a collection of files and folders to a file archive. The inability to select both from a single standard dialog is annoying. Reorganizing the app around "you can either add files or folders, but not both" doesn't make sense. I gather custom dialogs are the only option. – fadden Dec 01 '14 at 23:22
  • 3
    I totally afree with fadden, it is not about "organizing an applicatin better", there are use cases that simply require a user to be able to select both files AND folders, and it really is a pain that there is no Microsoft dialog supporting this :( – Erik May 07 '15 at 06:51
  • 3
    @Erk **[SHBrowseForFolder](https://learn.microsoft.com/en-us/windows/desktop/api/shlobj_core/nf-shlobj_core-shbrowseforfoldera)** supports showing files while selecting a folder; by using [**BIF_BROWSEINCLUDEFILES**](https://learn.microsoft.com/en-us/windows/desktop/api/shlobj_core/ns-shlobj_core-_browseinfoa). It's a shame that **IFileDialog** doesn't support the features from the API it was designed to supercede. – Ian Boyd Apr 22 '19 at 18:53
6

Sadly not possible right now, and Microsoft appears to be ignoring a request for the functionality: http://social.msdn.microsoft.com/Forums/en/windowsuidevelopment/thread/4a330e26-4d52-4fce-8a89-5c56fa132688

Owen
  • 7,494
  • 10
  • 42
  • 52