Search Amazon:

Browse for Files, Folders, Computers and Printers

Using the SHBrowseForFolder API you can invoke a system dialog used to browse for files and folders on your hard drive as well as network computers and printers. While this method of browsing your file system does not provide as many options as using the Common Dialog control, it is fast and easy to use. What it does do is allow you to select computers and printers. You can set flags that tell the system what to browse for. Windows will enable and disable the OK button automatically depending on the flags set and the item selected.


Download Source Code

Browse Dialog

The main component of this program is the SHBrowseForFolder API. SHBrowseForFolder displays a dialog box enabling the user to select a shell folder. To do this we must first populate a BrowseInfo structure which is used by the API. Two of the main elements of BrowseInfo are the lpszTitle and ulFlags fields. As the name implies, lpszTitle contains the title to display at the top of the dialog. The ulFlags element get the value which determines what the dialog displays and allows the user to select.

For example, to browse for folders only, ulFlags should contain the value BIF_RETURNONLYFSDIRS. To browse for both folders and files the field should be set to BIF_RETURNONLYFSDIRS Or BIF_BROWSEINCLUDEFILES. Once BrowseInfo is populated, the SHBrowseForFolder API is called to display the dialog.

You can navigate through the dialog but Windows will only allow you to select the type of item indicated by ulFlags. Once you click an item SHBrowseForFolder returns with ID that item. The ID must then be converted into a string representing the path to the chosen item. This is where the SHGetPathFromIDList API comes in. This function converts an item identifier to a file system path.

You can specify the folder to start in to a certain degree. You cannot specify just any folder. It must be a Window's Special Folder such as the Desktop, Program Files, Recycle Bin, Windows directory,...

Window's refers to special folders by means of an ID. A call to SHGetSpecialFolderLocation passing the handle of your form and a flag identifying the folder to locate populates an ITEMIDLIST structure with the ID of the folder. This ID can then be assigned to the pIDLRoot element of the BrowseInfo structure.

Finally, the item identifier list returned by SHBrowseForFolder must be freed via a call to the CoTaskMemFree function.

Applies To

Some of the flags and features may not be available depending on the version of Shell32.dll you have installed. Check out MSDN (there is a link on my Microsoft Links page) for details.

Instructions

Download the source code and press F5 to run the program. Select the option to browse for then click Browse. Experiment by setting different flag combinations.

Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Be the first to add a comment! Items to Show:     

     
You must log in to post feedback.
Comment:    
 

If you use this code, please mention "www.TheScarms.com"

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms