GingerJack
GingerJack

Reputation: 3134

How to select specific file using CfileDialog in MFC

I would like to open a file dialog and allow user to select only the file with name "myapplication.ini" and user can only browse folder's to check if the file is existing to select it.

so i came across CFileDialog which would do almost what i want other than limiting it to display only files with name "myapplication.ini"

currently my usage of CFiledialog

CFileDialog FileDialog(TRUE,"features.ini", NULL,OFN_HIDEREADONLY,NULL);

I am not sure what could should be changed to make it work as i expected.

Upvotes: 1

Views: 2763

Answers (2)

Hawk89
Hawk89

Reputation: 233

Declare the filter string like this:

static TCHAR BASED_CODE szFilter[] = _T("features.ini (features.ini)|features.ini|");

and then pass it to your CFileDialog ctor:

CFileDialog FileDialog(TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter);

Upvotes: 3

David Heffernan
David Heffernan

Reputation: 613572

That sounds like a poor UI. Even if you filter out all but that file, the user can override the filter. If you aren't going to allow the user to make a choice of file name, why ask them for their choice?

What you are actually doing, in my view, is asking the user to select a folder. So instead of the file dialog, show them a folder selection dialog, CFolderPickerDialog.

Upvotes: 3

Related Questions