Dialog for Selecting Directories

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Introduction

The CDirDialog class is an extension to MFC that allows selection of a directory with several nice user-interface features.

The class is derived from the MFC CFileDialog class. The code was built and tested with VC6 but should work with recent earlier versions of MFC, and probaly later ones too.

What it Does

The Microsoft recommended way to select a directory (folder) is to use SHBrowseForFolder. This facility is rather primitive for many reasons. For example, there is no way to create or delete a directory, view the files in the directory etc. As CFileDialog (and hence CDirDialog) uses the standard file open dialog you can do these things and more. One thing I really like is that the dialog is resizable under Windows 95/98 so you can expand it to see a lot more files/directories.

Since writing this I have found there is similar code on CodeGuru that provides a directory selection facility using CFileDialog. I think this one is better as it avoids a few problems and bugs and has the following extra features:

  • The file display area shows the contents of the current folder as the user types a full path. (Updated when a backslash or Enter is typed.)
  • Directory name completion means that if the characters type are the first characters of an existing directory then the rest of the directory name is displayed so the user can just press Enter. The extra characters are displayed selected so the user can easily override them if desired.
  • If a directory does not exist the user has the otpion of creating it. This will create all ancestor directories that do not exist.
  • The user has the option of toggling the display of files. By default only directories are displayed but the user can select to display different type of files from the "Display Files of Type" drop down list. (This is unlike the others where the display of files can be turned on or off only under programmer, not user, control.)
  • If files are displayed the user cannot select one.
  • Double-clicking a directory name does not select the directory but simply changes the display to show the contents of the directory. To actually select the directory the user must click the Open button.
  • Helpful error messages that explain why a drive name or path is invalid.

How to Use

To use the class just copy and include the DirDialog.h and DirDialog.cpp files into your project. You also need to link with IMAGEHLP.LIB as the standard Windows IMAGEHLP.DLL is called.

To use the dialog create an object of type CDirDialog and call its DoModal member function. In the same way you would for CFileDialog. The constructor takes 3 parameters: the initial directory, a set of file filters (as is often supplied to the CFileDialog constructor) and a pointer to the dialog's parent window. These parameters are optional and default to the current directory, displaying either all files (*.*) or no files (folders only), and no parent window.

The demo project shows how to use the CDirDialog class. The code for the class is included in the demo project. The demo executable is in the Release directory.

See DirDialog.cpp for more info.

Downloads

Download demo and source code project - 27 Kb


Comments

  • Ms

    Posted by Yamini on 03/11/2015 09:29am

    Perfect... i was searching for this.. Thanks a lot..

    Reply
  • Resizing problem

    Posted by Yenny Suryadi on 10/24/2012 02:49am

    I have tried your CDirDialog class, and it works perfectly, it can open dialog for folder only. My problem is, when I tried to resize the CDirDialog becoming bigger, the edit folder path won't resize according to the dialog width. Why?

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

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date