Dialog for Selecting Directories


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.


Download demo and source code project - 27 Kb


  • Ms

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

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

  • GHD Close To You 2013

    Posted by pykodq519 on 07/16/2013 08:16am

    Le redresseur a un attachement de fil en rotation et trois réglages de température différents pour les différentes caractéristiques et les types de cheveux, où la température maximale est de 230 ° convertisseur de tension globale C.Med pour 120-240V est le redresseur trop parfait pour avoir le voyage, et de le stocker en toute sécurité dans le sac résistant à la chaleur inclus dans luxe design.Elegant redresseurs Paris que vous pouvez à la fois les cheveux lisses et boucle avec. [url=http://ghdpascherferfr.blinkweb.com/]ghd lisseur prix[/url] Quand on sait ghd Norvège peut créer une variété de cheveux de style. Donc, nous voulons tous avoir un tel outil. Pour nous permettre easilycreate boucles et les vagues, ainsi que les cheveux raides parfait. Mais vous savez ce lisseur ghd sont le meilleur choix pour vous. Nous devons choisir lisseur ghd approprié pour vous, plutôt que de suivre le chemin de quelqu'un d'autre de l'acheter. [url=http://bbeatsinorge.cabanova.com/]ghd lisseur pas cher[/url] GHD droit tige conteneur livfullheten et lambeaux de cheveux Farget. Près de la rivière pour s'assurer que la chaleur est répartie uniformément, ne devient jamais Hoy, reduserer La fargebevaringsteknologi unique avec IONTEC décoloration av Fargen en stylisme de 70% * et rend le Fargen obtient appareils de coiffage beaucoup plus glansfull.GHD ne fait pas ditt de cheveux terne et sec. Moi, sur IMot: ils empêchent fuktighetstap de style par y infliger ditt de cheveux satengioner unique! Ces ladde négatif ionène célébrations segment à la chevelure ladde positif ditt et reduserer sec, krusete cheveux et tiltrekker fuktighetspartikler de Lufta et leur envoyer votre le Trenger de cheveux le plus.

  • 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?

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