I want to be able to select a folder on my HD and put the path into a text field. This should be easy enough except I can't get the CFileDialog to allow me to just select a folder. It needs me to select a file. I assume there's a flag that specifies you don't need to select a file, but I can't figure out what the flag is. I've checked msdn and can't see it. I have done it easily enough before with Win32, but can't seem to get CFileDialog to do it.

I tried creating the dialog as follows:

All this does is specify that the file doesn't have to be there. This way, I can navigate to the folder, type in any randon gibberish, and when the dialog closes, extract the folder path. This is a little messy and would rather be able to just select the folder.

Any ideas what I'm doing wrong?
Who is Participating?
bastibartelConnect With a Mentor Commented:
Hi there,

You cannot specify !OFN_FILEMUSTEXIST as a flag to CFileDialog::CFileDialog ()
The flags cannot be negated.

CFileDialog as it is does not allow folder selection.

Check this out (EE):

or (Codeproject)

mahesh1402Connect With a Mentor Commented:
>>I want to be able to select a folder on my HD and put the path into a text field.

Any reason you dont want to use 'Browse For Folder' Dialog using SHBrowseForFolder() ?

Otherwise look at this 'XFolderDialog - a folder selection dialog based on CFileDialog'


OOPs ....not looked.....XFolderDialog already given by bastibartel ..

what you can do is to derive a class of your own using the CFileDialog class as base class. Then in the header file of the class you just created just replace the constructor with the following.

YourClass::YourClass(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
            DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
            CFileDialog(bOpenFileDialog, lpszDefExt, "Don't Type Anything here", dwFlags, lpszFilter, pParentWnd)

then override the virtual function "OnFileNameOK()" of CFileDialog and give the following body to this function

BOOL YourClass::OnFileNameOK()
      return 0;

and thats it, now whenever u need the dialog invoke it in this way

YourClass m_fdFilePath(FALSE, NULL, "", OFN_HIDEREADONLY, NULL);

but u will have to parse the folder path which can be done in the following way

int i;
CString strm_fdFilePath.GetPathName()
if(i > 0)
      str.SetAt(i, '\0');

and presto, u will get the folder path u have just selected.

you can browse for a folder using about 4 lines of code;

void MyApp::OnButton1() {

char folderName[512];
BROWSEINFO browseInfo = { 0 };

LPITEMIDLIST itemIdList = SHBrowseForFolder(&browseInfo);
SHGetPathFromIDList(itemIdList, folderName);

if (strlen(folderName)) {
// a folder was selected and it's path is here
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.