Click here to Skip to main content
Licence 
First Posted 16 Feb 2006
Views 84,219
Bookmarked 49 times

How to Browse for a Folder

By | 16 Feb 2006 | Article
This article presents a 'cut-n-paste' solution for getting the path to a directory.

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

About the Author

Nitron

Engineer

United States United States

Member

Walter Storm is currently a research engineer for a large aircraft company. Originally from Tunkhannock, PA., he has a B.S. in Aerospace Engineering from Embry-Riddle Aeronautical University[^] with a minor in Psychology, and an M.S. in Systems Engineering from SMU[^]. He has been developing in C++ nearly full time since January of 2001. He is comfortable using MFC and has been known to hack at the STL with the best of them. He tends to be a perfectionist; especially when it comes to GUI design. When not integrating and testing flight control software or hacking at National Instruments'[^] software libraries, you can probably find him driving around in his 1990 2+2 Z32[^] (ok, another pic: Big Grin | :-D ) If by chance he's not in the Z, you may want to check for him here[^].
 
View Walter Storm's profile on LinkedIn.[^]

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberMember 143041120:07 6 Jul '10  
GeneralMFC - UNICODE Version Pinmemberexp2lnt17:20 19 Oct '08  
GeneralThanks Pinmemberpapa_coder8:41 18 Mar '06  
GeneralRe: Thanks PinmemberNitron5:12 4 Apr '06  
Questionbigger path? PinmembereXplodus19:54 16 Feb '06  
AnswerRe: bigger path? PinmemberNitron5:13 17 Feb '06  
GeneralRe: bigger path? PinmembereXplodus20:53 19 Feb '06  
GeneralRe: bigger path? PinmemberGalatei7:43 22 Feb '06  
QuestionPossible to preset a specific folder? PinmemberChristof Schardt11:38 16 Feb '06  
AnswerRe: Possible to preset a specific folder? PinmemberNitron11:44 16 Feb '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120517.1 | Last Updated 16 Feb 2006
Article Copyright 2006 by Nitron
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid