Click here to Skip to main content
12,897,483 members (48,286 online)
Click here to Skip to main content
Add your own
alternative version


53 bookmarked
Posted 16 Feb 2006

How to Browse for a Folder

, 16 Feb 2006
Rate this:
Please Sign up or sign in to vote.
This article presents a 'cut-n-paste' solution for getting the path to a directory.


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


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


  • 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.
   memset(&bi, 0, sizeof(bi));

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

   // must call this if using BIF_USENEWUI

   // 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


   return retVal;


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!


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


About the Author

Software Developer (Senior) Lockheed Martin
United States United States
Walter Storm is currently a principal software engineer doing quantitative research for a private hedge fund. 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.[^]

You may also be interested in...

Comments and Discussions

AnswerRe: Possible to preset a specific folder? Pin
Stephan Poirier21-Feb-06 18:12
memberStephan Poirier21-Feb-06 18:12 
AnswerRe: Possible to preset a specific folder? Pin
brightatlantis16-Oct-07 4:26
memberbrightatlantis16-Oct-07 4:26 
GeneralLooks better than the previous version but ... Pin
Roland Pibinger16-Feb-06 10:27
memberRoland Pibinger16-Feb-06 10:27 
GeneralRe: Looks better than the previous version but ... Pin
Nitron16-Feb-06 10:32
memberNitron16-Feb-06 10:32 
Roland Pibinger wrote:
MSDN: "To close the COM library gracefully, each successful call to OleInitialize, including those that return S_FALSE, must be balanced by a corresponding call to OleUninitialize."

Sigh | :sigh:

Roland Pibinger wrote:
CoTaskMemFree is the counterpart to SHGetPathFromIDList?

Well, here's how it works (so I think)... The ::SHBrowseForFolder will return a pointer to a ITEMIDLIST that's created using CoTaskAlloc under the hood, and the user is responsible for freeing the memory if the returned pointer is not NULL. So in essence, CoTaskMemFree is freeing allocated memory from SHBrowseForFolder, not SHGetPathFromIDList.

ññòòïðïðB A

GeneralRe: Looks better than the previous version but ... Pin
Nitron16-Feb-06 10:37
memberNitron16-Feb-06 10:37 
GeneralRe: Looks better than the previous version but ... Pin
Nishant Sivakumar16-Feb-06 10:49
staffNishant Sivakumar16-Feb-06 10:49 
GeneralRe: Looks better than the previous version but ... Pin
Nitron16-Feb-06 11:39
memberNitron16-Feb-06 11:39 
GeneralRe: Looks better than the previous version but ... Pin
bob169727-Feb-07 18:34
memberbob169727-Feb-07 18:34 
GeneralRe: Looks better than the previous version but ... Pin
Nishant Sivakumar16-Feb-06 10:47
staffNishant Sivakumar16-Feb-06 10:47 
Generalcallback with Unicode Pin
copernican16-Feb-06 9:50
membercopernican16-Feb-06 9:50 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 16 Feb 2006
Article Copyright 2006 by Nitron
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid