Visual Basic Browse/ PIDL / CSIDL Routines
Implementing the SHBrowseForFolder: Browse for Folders Dialog
     
Posted:   Saturday May 17, 1997
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB4-32, Windows 95
OS restrictions:   None
Author:   Brad Martinez
     
Related:   BROWSE:
SHBrowseForFolder: Browse Folders Dialog Overview
SHBrowseForFolder: Browse for Folders Callback Overview
SHBrowseForFolder: Pre-selecting Folders using a Browse Callback
SHBrowseForFolder: Browse for Folders New UI Features
SHBrowseForFolder: Browse to Obtain Network Machines or Shares

CSIDL / Folders:
SHGetFolderPath: Overview of Shell and ComCtrl Versions, CSIDL Values
SHGetFolderPath: Retrieve Windows Shell Folders (Best Practice)

SHGetSpecialFolderLocation: Retrieve Windows Shell Folder
SHGetKnownFolderPath: Retrieve Windows Known (Shell) Folders under Vista
Pure VB: Using the Shell Application Object to Retrieve Windows Shell Folders

   
 Prerequisites
None.

Possibly one of the 'top ten' newsgroup questions, the code here shows the correct technique to call the Browse dialog from within a VB app, based on code authored and provided to VBnet by Brad Martinez.
 BAS Module Code
None.

 Form Code
To a project form add two command buttons (Command1 and Command2), and a label (Label1) as indicated in the illustration. Add the following to the form:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Type BROWSEINFO
   hOwner           As Long
   pidlRoot         As Long
   pszDisplayName   As String
   lpszTitle        As String
   ulFlags          As Long
   lpfn             As Long
   lParam           As Long
   iImage           As Long
End Type

Private Const BIF_RETURNONLYFSDIRS = &H1
Private Const BIF_DONTGOBELOWDOMAIN = &H2
Private Const BIF_STATUSTEXT = &H4
Private Const BIF_RETURNFSANCESTORS = &H8
Private Const BIF_BROWSEFORCOMPUTER = &H1000
Private Const BIF_BROWSEFORPRINTER = &H2000
Private Const MAX_PATH As Long = 260

Private Declare Function SHGetPathFromIDList Lib "shell32" _
   Alias "SHGetPathFromIDListA" _
  (ByVal pidl As Long, _
   ByVal pszPath As String) As Long

Private Declare Function SHBrowseForFolder Lib "shell32" _
   Alias "SHBrowseForFolderA" _
  (lpBrowseInfo As BROWSEINFO) As Long

Private Declare Sub CoTaskMemFree Lib "ole32" _
   (ByVal pv As Long)



Private Sub Command1_Click()

  Dim bi As BROWSEINFO
  Dim pidl As Long
  Dim path As String
  Dim pos As Long
    
  Label1.Caption = ""

 'Fill the BROWSEINFO structure with the 
 'needed data. To accommodate comments, the 
 'With/End With syntax has not been used, though 
 'it should be your 'final' version.

   With bi
      
     'hwnd of the window that receives messages 
     'from the call. Can be your application 
     'or the handle from GetDesktopWindow()      
      .hOwner = Me.hWnd

     'pointer to the item identifier list specifying
     'the location of the "root" folder to browse from.
     'If NULL, the desktop folder is used.
     .pidlRoot = 0&

     'message to be displayed in the Browse dialog
     .lpszTitle = "Select your Windows\System\ directory"

     'the type of folder to return.
      .ulFlags = BIF_RETURNONLYFSDIRS
   End With
    
  'show the Browse Dialog
   pidl = SHBrowseForFolder(bi)
 
  'the dialog has closed, so parse & display the
  'user's returned folder selection contained in pidl
   path = Space$(MAX_PATH)
    
   If SHGetPathFromIDList(ByVal pidl, ByVal path) Then
      pos = InStr(path, Chr$(0))
      Label1.Caption = Left(path, pos - 1)
   End If

   Call CoTaskMemFree(pidl)
  
End Sub
 Comments
Running the project, the folder you select in the Browse dialog will be returned to the label.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy