CDirSelDialog : Folder Selection Dialog | CodeGuru

CDirSelDialog : Folder Selection Dialog

CDirSelDialog is derived from CFileDialog. It is customized to show only directories, and it has the following additional features : You can optionally see the files in a directory, before you decide to choose one directory for a particular purpose. This feature was lacking in previous versions of directory selection dialogs, either a class was […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 23, 2001
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

CDirSelDialog is derived from CFileDialog. It is customized to show only directories, and
it has the following additional features :

  • You can optionally see the files in a directory, before you decide to choose one
    directory for a particular purpose. This feature was lacking in previous versions
    of directory selection dialogs, either a class was derived from CFileDialog or
    a class abstacted the API function SHBrowseForFolder.
  • In the editbox, you can type in directory name or .. or whatever, which you want
    to navigate to. However, CDirSelDialog will not let you type the masks, like *.h or *.*
    etc, to view the files within the listview control. If you want to see the files,
    you can view them in the separate listbox, for which no mask is used, *.* being used always.
  • You can choose to navigate to the previous directories, which you have visited earlier,
    using the bottom combobox.
  • From the ComboBox, you can copy the directory name into clipboard if reqd.
  • Finally, thanks to Mihai Filimon for

    his article on Folder Dialog
    , providing the windowproc hook to start with. In his
    article, he had used “*..*” intentionally to avoid showing files in the listview control.
    However, I have used “.*” as file mask for the listview control, to get better results.

    Sample Usage Code for CDirSelDialog would be

    CDirSelDialog dirdlg;
    if( dirdlg.DoModal() == IDOK )
    {
     AfxMessageBox( dirdlg.GetDirName() );
    }
    

    Download demo & source code – 22 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.