Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / MFC
Article

How to Browse for a Folder

Rate me:
Please Sign up or sign in to vote.
4.88/5 (20 votes)
16 Feb 20061 min read 225.7K   2.9K   55   49
This article presents a 'cut-n-paste' solution for getting the path to a directory.

Image 1

Introduction

This article presents a no-frills, cut-n-paste solution to the age-old problem of browsing for a folder. There are several classes out here that offer much more customization and a plethora of features, but I'm sure one more example won't hurt.

When to use this solution

The benefit of this approach over some of the others here is that this implementation is a direct wrapper function for SHBrowseForFolder. As such, you can use it in console applications that don't use MFC, such as unit-testing applications that could benefit from not hard-coding an output location for test results.

So, in short, if you're looking for eye candy, check out XFolderDialog or some of the other folder dialogs here. Otherwise, read on.

Using the code

To use this code, you need to include the following standard files:

#include "shlobj.h"
#include <string>

The GetFolder function

Params

  • folderpath - A reference to the string that will contain the folder path if successful.
  • szCaption - An optional message for the dialog.
  • hOwner - An optional handle to the parent window.

Returns

  • true if successful, false if cancelled or an error occurs.
bool GetFolder(std::string& folderpath, 
               const char* szCaption = NULL, 
               HWND hOwner = NULL)
{
   bool retVal = false;

   // The BROWSEINFO struct tells the shell 
   // how it should display the dialog.
   BROWSEINFO bi;
   memset(&bi, 0, sizeof(bi));

   bi.ulFlags   = BIF_USENEWUI;
   bi.hwndOwner = hOwner;
   bi.lpszTitle = szCaption;

   // must call this if using BIF_USENEWUI
   ::OleInitialize(NULL);

   // Show the dialog and get the itemIDList for the 
   // selected folder.
   LPITEMIDLIST pIDL = ::SHBrowseForFolder(&bi);

   if(pIDL != NULL)
   {
      // Create a buffer to store the path, then 
      // get the path.
      char buffer[_MAX_PATH] = {'\0'};
      if(::SHGetPathFromIDList(pIDL, buffer) != 0)
      {
         // Set the string value.
         folderpath = buffer;
         retVal = true;
      }

      // free the item id list
      CoTaskMemFree(pIDL);
   }

   ::OleUninitialize();

   return retVal;
}

Conclusion

Well, that's really all there is to it. If this code saves time for even one person out here, then the few minutes spent writing this was well worth it. Enjoy!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
CEO Aspen Insights
United States United States
Walter Storm is currently doing quantitative research and data science. Originally from Tunkhannock, PA., he has a B.S. in Aerospace Engineering from Embry-Riddle Aeronautical University[^], and an M.S. in Systems Engineering from SMU[^]. He has been professionally developing software in some form or another since January of 2001.

View Walter Storm's profile on LinkedIn.[^]

Comments and Discussions

 
QuestionIs it possible to add a second rootFolder? Pin
Laudenberg15-Apr-15 22:40
Laudenberg15-Apr-15 22:40 
AnswerRe: Is it possible to add a second rootFolder? Pin
Laudenberg20-Apr-15 3:06
Laudenberg20-Apr-15 3:06 
GeneralWorks great on Windows 8.1... Pin
abdekker12325-Mar-15 1:33
abdekker12325-Mar-15 1:33 
QuestionHow to suppress dialog popups during mousover tree items. Pin
Vytas Adomkaitis16-Dec-14 8:49
Vytas Adomkaitis16-Dec-14 8:49 
GeneralMy vote of 5 Pin
WebMaster17-Jan-13 14:54
WebMaster17-Jan-13 14:54 
GeneralMy vote of 5 Pin
Member 14304116-Jul-10 20:07
Member 14304116-Jul-10 20:07 
GeneralMFC - UNICODE Version Pin
exp2lnt19-Oct-08 17:20
exp2lnt19-Oct-08 17:20 
bool GetFolder(CString& csFolder, CString csCaption = NULL, HWND hWndOwner = NULL)
{
bool bSuccess = false;

// The BROWSEINFO struct tells the shell
// How it should display the dialog.
BROWSEINFO browseInfo;
memset(&browseInfo, 0, sizeof(browseInfo));

browseInfo.ulFlags = BIF_USENEWUI;
browseInfo.hwndOwner = hWndOwner;
browseInfo.lpszTitle = csCaption;

// Must call this if using BIF_USENEWUI
::OleInitialize(NULL);

// Show the dialog and get the itemIDList for the selected folder.
LPITEMIDLIST pIDL = ::SHBrowseForFolder(&browseInfo);

if(pIDL != NULL)
{
// Create a buffer to store the path, then get the path.
TCHAR lpzTempFolder[MAX_PATH + 1];
if(::SHGetPathFromIDList(pIDL, lpzTempFolder) != 0)
{
bSuccess = true;
}

// Free the item id list
CoTaskMemFree(pIDL);
csFolder = lpzTempFolder;
}

::OleUninitialize();

return bSuccess;
}

Hope this helps,
theme
GeneralThanks Pin
papa_coder18-Mar-06 8:41
papa_coder18-Mar-06 8:41 
GeneralRe: Thanks Pin
Nitron4-Apr-06 5:12
Nitron4-Apr-06 5:12 
Questionbigger path? Pin
eXplodus16-Feb-06 19:54
eXplodus16-Feb-06 19:54 
AnswerRe: bigger path? Pin
Nitron17-Feb-06 5:13
Nitron17-Feb-06 5:13 
GeneralRe: bigger path? Pin
eXplodus19-Feb-06 20:53
eXplodus19-Feb-06 20:53 
GeneralRe: bigger path? Pin
Galatei22-Feb-06 7:43
Galatei22-Feb-06 7:43 
QuestionPossible to preset a specific folder? Pin
Christof Schardt16-Feb-06 11:38
Christof Schardt16-Feb-06 11:38 
AnswerRe: Possible to preset a specific folder? Pin
Nitron16-Feb-06 11:44
Nitron16-Feb-06 11:44 
GeneralRe: Possible to preset a specific folder? Pin
Christof Schardt16-Feb-06 11:50
Christof Schardt16-Feb-06 11:50 
GeneralRe: Possible to preset a specific folder? Pin
Nish Nishant16-Feb-06 14:18
sitebuilderNish Nishant16-Feb-06 14:18 
GeneralRe: Possible to preset a specific folder? Pin
Christof Schardt16-Feb-06 19:56
Christof Schardt16-Feb-06 19:56 
GeneralRe: Possible to preset a specific folder? Pin
Nish Nishant17-Feb-06 1:23
sitebuilderNish Nishant17-Feb-06 1:23 
GeneralRe: Possible to preset a specific folder? Pin
SBJ21-Feb-06 4:52
SBJ21-Feb-06 4:52 
GeneralRe: Possible to preset a specific folder? Pin
Nish Nishant21-Feb-06 13:15
sitebuilderNish Nishant21-Feb-06 13:15 
GeneralRe: Possible to preset a specific folder? Pin
SBJ21-Feb-06 13:27
SBJ21-Feb-06 13:27 
AnswerRe: Possible to preset a specific folder? Pin
Nitron17-Feb-06 3:32
Nitron17-Feb-06 3:32 
AnswerRe: Possible to preset a specific folder? Pin
Stephan Poirier21-Feb-06 18:12
Stephan Poirier21-Feb-06 18:12 
AnswerRe: Possible to preset a specific folder? Pin
brightatlantis16-Oct-07 4:26
brightatlantis16-Oct-07 4:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.