Home Search Contact us About us
Title Directory and network browsing using a tree control
Summary An article on Browsing my computer and the network using a TreeCtrl. This is also a good example of information on network enumeration.
Contributor John McTainsh
Published 12-Jun-1999
Last updated 7-Sep-2000
Page rating   96% for 25 votes Useless Brilliant
 Download demo executable - 64 Kb
 Download project and source - 18 Kb
 Download SDI / CTreeView project sample - 22 Kb

Path Picker Dialog Image

This dialog class displays a directory tree similar to the one found in "Find in Files" Browse button. Shown above.

I have noticed several applications requiring a user to select a path that often use CFileDialog which does not fit the task very well. A better option is to use a Tree control but I have not found any that allow access to the network other than via mapped drives. Both methods lack the style that "Find in Files" provides. I have also spent some time trying to use SHBrowseForFolder but to date have had little success.

Why not just use SHBrowseForFolder.

Although SHBrowseForFolder provides all this functionality and more it does come with the following drawbacks.
  • Documentation is difficult to understand and lacks good examples.
  • Syntax is very cryptic.
  • Many lines of code plus callbacks are necessary to do simply tasks such as set the inital path.
  • The SHBrowseForFolder dialog can not be customized to any great extent. ie Changing the dialogs layout or Adding a control showing information about the selected directory is not possible.

How to use it.

I have developed a Dialog for easy implementation. The source is written to put the Dialog object into a single class so it can be hacked out and used in any willing developers code.

  1. Copy the source files DlgGetPath.cpp and DlgGetPath.h to your Project directory and add them to your project.
  2. From within your project open the TryNetworkTree.rc file and drag the Bitmap (IDB_FILE_TYPES) and Dialog (IDD_GET_PATH) into your project.
  3. Add the mpr.lib in to be linked into your application. To do this select Project->Settings to open the Project settings dialog. Select the Link tab and from the drop down box select the General Catagory. In the Object/Link modules edit type in mpr.lib
  4. Add the following code where you want to activate the Picker dialog. ie in a button handler.
//#include "DlgGetPath.h"          //Definition to get path
void CBlarOnButtonBrowse()
    CDlgGetPath dlgPath( this );
    dlgPath.SetPath( _T("C:\\Temp") );
    if( dlgPath.DoModal() == IDOK )
        //Do some cool stuff with it here
        //SetDlgItemText( IDC_EDIT_SEARCH_IN, dlgPath.GetPath() );

Using selection event.

When the user selects a branch in the tree it is possible to catch this event and use the selected path to perform some action. For example windows explorer uses the selection of a branch in the left pane to display the directory in the right pane. To do this place code at the end of CDlgGetPath::OnSelchangedTree at around line 391. The path is available and is displayed at the top of the dialog for this sample.

    //TODO add code here to process users selection
    ASSERT( GetDlgItem( IDC_STATIC_NOTE ) );
    SetDlgItemText( IDC_STATIC_NOTE, sPath ); 


  • Display the recycle bin appropiatly.
  • Display the contents of the recycle bin with out S- numbers.
  • Sort items when added except for "Entire Network".
  • Check on a more complex network.
  • Determine why icon disappears from the ALT-TAB bar when active?
  • Handle WNetEnumResource for more than 16384 bytes of data.
  • It would be nice to read the system icons with SHGetFileInfo and use them.


Date Posted: May 30, 1999 - CodeGuru

  • Inital release.

Date Posted: September 7, 2000 - Code Project

  • Added Sven Wiegand fix for GetDriveType.
  • Added SetPath( blar ); to allow the preset expansion of a given path.
  • Notes on SHBrowseForFolder (No code change)?
Comments Date
Urgent Help 21-Mar-2001 Sudalaimuthu.M
Respected Sir,
I am sudalai muthu from india. I am new to MFC. Just few days before i have seen your article. Really it is nice and helpful to me. Thanks a lot. I have downloaded your demo project from http://www.mctainsh.com and i have tried to added the file views when i clicked any one folder . I have succeded.
Now i want to display the current folder in side a combo box's edit box and the explorer view inside the list box of the combo box (Such like the combo box inside the windows explorer). I have tried for the past three days. But i couldn't. Please give me an idea to achieve this otherwise give me the demo project.

Please help me.
Thanks a lot.

Yours truly,
M.Sudalai muthu
nice code + one suggestion 23-Jul-2001 chris
this is the only code i found on many mfc pages in the net that has only good ratings.

there is only one suggestion:
i would add special folders like the desktop and workspace. the normal user expects the explorer like style.

additionally you could add multithreading and something like a status bar for folders with many files. but maybe this things will make it harder to understand the code...

thx a lot
monitoring of directories 26-Sep-2001 Evelyn Tristao
Hi, I need help in finding out how I can monitor a windows directory (local or on a network) for specific events, such as file creation and deletion. I did find something in MSN but it only works on NT and Win2K. I need something that will work on all windows platform at least.
Thanks much.
Odd Behaviour 23-May-2002 Dan Crosby
Using your browser, if you display folders from a FAT volume, they appear in an unsorted order. Folders on NTFS volumes appear in alphabetical order.
Comment I added to CodeProject 24-May-2002 Dan Crosby
John, I don`t know if you are updating this work or not, but just in case I implemented a node sorting method in the code I borrowed from you. Following is a note I added to your CodeProject entry.
I used an implementation of John`s excellent work and only had one minor problem. FAT volumes are not alphabetically sorted. To get around this you can modify John`s `InsertItem` routine by adding another argument thusly:

HTREEITEM InsertItem(CTreeCtrl *pTree, HTREEITEM hParent, NETRESOURCE *const pNetResource, CString sText, int iImage, int iImageSelected = -1, HTREEITEM hInsertAfter = TVI_LAST);

You also need to change this line:

InsertStruct.hInsertAfter = TVI_LAST;


InsertStruct.hInsertAfter = hInsertAfter;

Then in whatever calls you want (I did it for folder items and network share names) you can set the last argument of `InsertItem` to TVI_SORT so that those nodes will be sorted.

Home Search Contact us About us