SHARE
Facebook X Pinterest WhatsApp

Dialog for Selecting Directories

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 […]

Written By
thumbnail
CodeGuru Staff
CodeGuru Staff
Jan 14, 2000
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

Recommended for you...

How To Make Windows 11 Faster
Enrique Stone
Nov 30, 2022
Working with the Windows Registry in C#
Joydip Kanjilal
Jun 17, 2022
Using Multiple Programming Languages to Create an ASP.NET Website
Tariq Siddiqui
Jun 11, 2022
Finding a Microsoft Office version with .NET
Hannes DuPreez
May 20, 2022
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. © 2025 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.