I know that Microsoft recommends using the Common Item Dialog boxes over GetOpenFileName() and GetSaveFileName() but I am working with some older code.
How can I limit GetOpenFileName() to only allow the user to select files in a certain directory?
For example, I only want the user to be able to select a file from the “Images” directory. I don’t want them to be able to navigate into other directories when opening a file. They must select a file from the “Images” directory.
The flag OFN_NOCHANGEDIR sounds like it could be what I need, but that just changes the current directory back to the original directory if the user changes it when opening a file.
If it’s not possible with GetOpenFileName(), is it possible with the newer Common Item Dialog?
For
GetOpenFileName()andGetSaveFileName(), you can assign a callback procedure to theOPENFILENAME::lpfnHookfield. When it receives aCDN_FILEOKnotification, retrieve the selected folder by sending the dialog aCDM_GETFOLDERPATHmessage. If the folder is not acceptable to you, display a message to the user and then reject the selected file(s) by callingSetWindowLong()to set a nonzeroDWL_MSGRESULTvalue, and return a nonzero value from the hook procedure.Update: for
IFileOpenDialogandIFileSaveDialog, you can implement theIFileDialogEvents::OnFolderChangingevent callback to prevent navigation to unwanted folders, by simply returning an errorHRESULTvalue other thanS_OKorE_NOTIMPL.