Mircea Ispas
Mircea Ispas

Reputation: 20780

How to restrict FileDialog to specific path

Is it possible to restrict a file dialog(open/save) to a specific folder is winapi?

OPENFILENAME fileDialogSettings;
...
fileDialogSettings.lpstrInitialDir = "Some path";
...
if(GetOpenFileName(&fileDialogSettings))
{
}

I want to have "Some path" as root path in the dialog and to restrict navigation to this folder and it's sub folders only. May I use lpfnHook for this?

Upvotes: 3

Views: 3757

Answers (2)

Barış Uşaklı
Barış Uşaklı

Reputation: 13532

Look into OFN_NOCHANGEDIR flag, although the documentation says this:

Restores the current directory to its original value if the user changed the directory while searching for files.

This flag is ineffective for GetOpenFileName.

Edit: Reading your question again, I guess you don't want the user to navigate up from that directory, not sure if this is possible with GetOpenFileName, you might have to create your own dialog with a directory list view and restrict them that way.

Upvotes: 0

Deanna
Deanna

Reputation: 24253

If you're targeting Vista+ only, you can make use of the IFileDialogEvents::OnFolderChanging method to block the change altogether.

For older versions of Windows, the OpenFileDialog allows you to specify a hook procedure in which you can pick up on the CDN_FOLDERCHANGE notification. While I can't see any message to disallow the change, you may be able to post a message to tell it to go "back", or just disable the "OK" button.

Another option is to handle CDN_FILEOK notification and refuse paths outside your required directory.

See this MSDN article for more details about the hook procedure. This question also talks about changing the directory in an open dialog.

Upvotes: 2

Related Questions