Dialog for Selecting (and Creating) Folders

Class Purpose

The CPathDialog class lets users select or create a folder. It's just a simple extension of SHBrowseForFolder function.


Class Constructor

 LPCTSTR lpszCaption=NULL,     //Caption of dialog
 LPCTSTR lpszTitle=NULL,       //Prompt of program
 LPCTSTR lpszInitialPath=NULL, //Initial path
 CWnd* pParent=NULL            //Parent of dialog

Display Dialog

int CPathDialog::DoModal();
CPathDialog returns the return value of the DoModal function (can be either IDOK or IDCANCEL).

Ensure Path Exists

int CPathDialog::MakeSurePathExists(
 LPCTSTR lpPath //Path to driectory you want to exist
The MakeSurePathExists function ensures that the path presented by the lpPath argument exists. If it does not exist, the function attempts to create it.
Returns of the fucntion can be:
  • -1: user cancels create the lpPath directory.
  • 0 : lpPath directory exists or was created.
  • 1 : lpPath is invalid.
  • 2 : can not create lpPath directory.

Integrating the CPathDialog into Your Project

  1. Import two files "pathdialog.h" and "pathdialog.cpp" into your project.
  2. Include the header file "pathdialog.h"
  3. Declare an object for CPathDialog class
  4. Invoke DoModal function to show dialog
  5. If user press OK button, get path name user's entered by using GetPathName function.

Example of Using the CPathDialog Class:

#include "PathDialog.h"
void CTestPathDialogDlg::OnBrowserButton()
 CString strInitialPath;
 CString strYourCaption(_T("Your caption here..."));
 CString strYourTitle(_T("Your tilte here..."));


 CPathDialog dlg(strYourCaption, strYourTitle, strInitialPath);

 if (dlg.DoModal()==IDOK)

void CTestPathDialogDlg::OnOKButton() 
 CString strPath;



Download demo project - 21 Kb


  • Nice piece of code that really should be part of MFC anyway

    Posted by Legacy on 02/18/2004 12:00am

    Originally posted by: J Rees

    Thanks a lot, exactly what i was looking for. Nice how it works just like the other file dialogs in mfc!

  • Enabling network resources

    Posted by Legacy on 05/19/2003 12:00am

    Originally posted by: Rob Kluver

    There is a simple workaround to get the class to recognize network resources as valid paths. 

    The code currently checks for a ':' to see if the path is a local folder. However,by allowing any path that is not a null string AND does not contain 'nethood\' in it, will permit the class to recognize network resources as well.

    If the user clicked on a link in the NetHood folder, the SHGetPathFromIDList() returns the path of the link. We therefore need to filter that out, since we don't want to handle that as a real folder. On Win2K, at least, a link to a network resource will normally contain the substring 'nethood' in the returned path. Also, since nethood is a valid folder we need to allow that to pass though, so 'nethood\' will filter out subfolders but not a path to NetHood itself.

    The code segment will now allow local paths and UNC (\\computer\dir) to pass through.

    The following code segment replaces some of this code in PathDialog.cpp(139):

    BOOL failed = FALSE;
    char szSelection[MAX_PATH];
    char szLwrSelection[MAX_PATH];

    szLwrSelection[0] = NULL;

    // Win2000: nethood is the name of folder with
    // shortcuts to other network resourses. The
    // folders are displayed, however they are
    // only links, and returned string must not be
    // treated as a folder.
    strcpy(szLwrSelection, szSelection);

    //Make lower-case for comparison

    if (szLwrSelection[0] != NULL &&
    (strstr(szLwrSelection, "nethood\\") == NULL))
    // Valid folder
    ::SendMessage(hwnd, BFFM_ENABLEOK, 0, TRUE);
    //Invalid folder
    szSelection[0] = '\0';
    ::SendMessage(hwnd, BFFM_ENABLEOK, 0, FALSE);

    ::GetDlgItem(hwnd, IDC_NEW_EDIT_PATH),

    Also, the following lines must be taken out since UNC paths will throw an exception:

    PathDialog.cpp (322):
    #if 0
    //validate path
    iRet=Touch(lpPath, TRUE);
    throw iRet;

  • Couldn't select the Network folder

    Posted by Legacy on 03/06/2003 12:00am

    Originally posted by: Simon Soosai

    When i choose a local hard disk folder OK button is enabled which is right.
    But when i go to select the Network folder OK button is disabled. How do i select a network folder?

  • What I needed!

    Posted by Legacy on 11/07/2002 12:00am

    Originally posted by: Anitox

    This is exactly what I needed. Thank you for your help. This code works great!

  • Compilation errors

    Posted by Legacy on 05/20/2002 12:00am

    Originally posted by: theFresh

    MyDlg.obj : error LNK2001: unresolved external symbol "public: class CString __thiscall CPathDialog::GetPathName(void)" (?GetPathName@CPathDialog@@QAE?AVCString@@XZ)
    MyDlg.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall CPathDialog::DoModal(void)" (?DoModal@CPathDialog@@UAEHXZ)
    MyDlg.obj : error LNK2001: unresolved external symbol "public: __thiscall CPathDialog::CPathDialog(char const *,char const *,char const *,class CWnd *)" (??0CPathDialog@@QAE@PBD00PAVCWnd@@@Z)
    Release/MyApp.exe : fatal error LNK1120: 3 unresolved externals
    Error executing link.exe.

  • Compiler error ;(

    Posted by Legacy on 03/13/2002 12:00am

    Originally posted by: Tutankhamen

    at line #108:

    E:\MyFolder\PathDialog.cpp(108) : warning C4003: not enough actual parameters for macro 'SubclassWindow'
    E:\MyFolder\PathDialog.cpp(108) : error C2059: syntax error : '('

  • Very nice

    Posted by Legacy on 03/07/2002 12:00am

    Originally posted by: Craig

    Works great. You really saved me some trouble.

  • Great job!!

    Posted by Legacy on 03/07/2002 12:00am

    Originally posted by: silver line

    Thank you. great job!!

  • Great Code, Thanks!!!!:)

    Posted by Legacy on 01/28/2002 12:00am

    Originally posted by: Joe Ersinghaus


  • network folder....disable

    Posted by Legacy on 12/30/2001 12:00am

    Originally posted by: Lee Gyu Seung


    i cann't it;

  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Download the Information Governance Survey Benchmark Report to gain insights that can help you further establish business value in your Records and Information Management (RIM) program and across your entire organization. Discover how your peers in the industry are dealing with this evolving information lifecycle management environment and uncover key insights such as: 87% of organizations surveyed have a RIM program in place 8% measure compliance 64% cannot get employees to "let go" of information for …

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds