Click here to Skip to main content
Click here to Skip to main content

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.

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

Share

About the Author

Nitron
Software Developer (Senior) Barbnet Investment Co.
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.[^]

Comments and Discussions

 
GeneralRe: Possible to preset a specific folder? PinstaffNishant Sivakumar21-Feb-06 13:15 
GeneralRe: Possible to preset a specific folder? PinmemberSBJ21-Feb-06 13:27 
AnswerRe: Possible to preset a specific folder? PinmemberNitron17-Feb-06 3:32 
Here is a version that allows you to specify a starting location, but you cannot go up from it, only down:
 
bool GetFolder(std::string& folderpath, LPCTSTR szCaption = NULL, LPCWSTR szStartPath = NULL, HWND hOwner = NULL)
{
	bool retVal = false;
 
	// get a ITEMIDLIST for the starting path
	LPITEMIDLIST  pIDLRoot  = NULL;
 
	if (szStartPath != NULL)
	{
		LPSHELLFOLDER pShellFolder = NULL;
		HRESULT       hResult      = NULL;
		ULONG         chUsed       = 0L;
		BSTR          bstrPath     = NULL;
 
		bstrPath = ::SysAllocString ( szStartPath );
 
		if ( NULL != bstrPath )
		{
			// Get desktop IShellFolder interface
			if (::SHGetDesktopFolder (&pShellFolder) == NOERROR)
			{
				// convert the path to an ITEMIDLIST
				hResult = 
					pShellFolder->ParseDisplayName 
						(
							NULL,			// owner window
							NULL,			// reserved (must be NULL)
							bstrPath,       	// folder name
							&chUsed,		// number of chars parsed
							&pIDLRoot,      // ITEMIDLIST
							NULL            // attributes (can be NULL)
						);
 
				if (FAILED(hResult))
				{
					pIDLRoot = NULL;
				}
 
				pShellFolder->Release();
			}
		}
 
		::SysFreeString(bstrPath);
	}
 
	// The BROWSEINFO struct tells the shell 
	// how it should display the dialog.
	BROWSEINFO bi;
	memset(&bi, 0, sizeof(bi));
 
	bi.pidlRoot  = pIDLRoot;
	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;
}

 
~Nitron.
ññòòïðïðB A
start

AnswerRe: Possible to preset a specific folder? PinmemberStephan Poirier21-Feb-06 18:12 
AnswerRe: Possible to preset a specific folder? Pinmemberbrightatlantis16-Oct-07 4:26 
GeneralLooks better than the previous version but ... PinmemberRoland Pibinger16-Feb-06 10:27 
GeneralRe: Looks better than the previous version but ... PinmemberNitron16-Feb-06 10:32 
GeneralRe: Looks better than the previous version but ... PinmemberNitron16-Feb-06 10:37 
GeneralRe: Looks better than the previous version but ... PinstaffNishant Sivakumar16-Feb-06 10:49 
GeneralRe: Looks better than the previous version but ... PinmemberNitron16-Feb-06 11:39 
GeneralRe: Looks better than the previous version but ... Pinmemberbob169727-Feb-07 18:34 
GeneralRe: Looks better than the previous version but ... PinstaffNishant Sivakumar16-Feb-06 10:47 
Generalcallback with Unicode Pinmembercopernican16-Feb-06 9:50 
GeneralComments PinstaffNishant Sivakumar16-Feb-06 6:51 
GeneralRe: Comments PinstaffNishant Sivakumar16-Feb-06 6:54 
GeneralRe: Comments PinstaffNishant Sivakumar16-Feb-06 7:00 
GeneralRe: Comments PinstaffNishant Sivakumar16-Feb-06 7:01 
GeneralRe: Comments PinmemberPJ Arends16-Feb-06 7:13 
GeneralRe: Comments PinstaffNishant Sivakumar16-Feb-06 7:17 
GeneralRe: Comments PinmemberNitron16-Feb-06 7:50 
GeneralRe: Comments PinmemberNitron16-Feb-06 7:44 
GeneralRe: Comments PinmemberNitron16-Feb-06 7:46 
GeneralRe: Comments PinstaffNishant Sivakumar16-Feb-06 8:32 
GeneralRe: Comments PinmemberNitron16-Feb-06 8:32 
GeneralRe: Comments PinmemberNitron16-Feb-06 9:11 

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.

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 16 Feb 2006
Article Copyright 2006 by Nitron
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid