Dialog for Selecting (and Creating) Folders | CodeGuru

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. Functions Class Constructor CPathDialog::CPathDialog( 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 […]

Written By
CodeGuru Staff
CodeGuru Staff
May 11, 2000
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Class Purpose

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

Functions

Class Constructor

CPathDialog::CPathDialog(
 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.
Advertisement

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…”));
 m_ctrlPathName.GetWindowText(strInitialPath);
 CPathDialog dlg(strYourCaption, strYourTitle, strInitialPath);
 if (dlg.DoModal()==IDOK)
 {
  m_ctrlPathName.SetWindowText(dlg.GetPathName());
 }
}
void CTestPathDialogDlg::OnOKButton()
{
 CString strPath;
 m_ctrlPathName.GetWindowText(strPath);
 if(CPathDialog::MakeSurePathExists(strPath)==0)
 {
  CDialog::OnOK();
 }
}

Downloads

Download demo project – 21 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.