windowswinapidialoggetopenfilename

Select file OR folder from same dialog on Windows?


I'd like to allow users of my Windows game to use a dialog box to select a folder or a file. I can currently do one or the other using SHBrowseForFolder or GetOpenFileName, but not both options from within the same dialog. Is this possible? (Reason being I'm porting a Mac game where this is possible.)


Solution

  • When you bring up the file dialog, give it a default filename such as "Entire folder" and turn off the flag that requires the file to exist before enabling the OK button. When the dialog returns, check the filename to see if it matches the special string "Entire folder" and treat it accordingly.

    Based on my answer to another question: How do you configure an OpenFileDialog to select folders?