VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Choose Directory

by Kamilche (35 Submissions)
Category: Files/File Controls/Input/Output
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (7 Votes)

Via API calls, has the user choose a directory. No commondialog needed!

Side Effects
Place this in a class to use.

Rate Choose Directory

Option Explicit
Private Const BIF_RETURNONLYFSDIRS = 1
Private Const BIF_DONTGOBELOWDOMAIN = 2
Private Const MAX_PATH = 260
Private Declare Function SHBrowseForFolder Lib "shell32" _
         (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" _
         (ByVal pidList As Long, _
         ByVal lpBuffer As String) As Long
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" _
         (ByVal lpString1 As String, ByVal _
         lpString2 As String) As Long
Private Type BrowseInfo
 hWndOwner  As Long
 pIDLRoot  As Long
 pszDisplayName As Long
 lpszTitle  As Long
 ulFlags  As Long
 lpfnCallback As Long
 lParam   As Long
 iImage   As Long
End Type

Friend Function GetFolderName() As String
'Opens a Treeview control that displays the directories in a computer
 Dim lpIDList As Long
 Dim sBuffer As String
 Dim szTitle As String
 Dim tBrowseInfo As BrowseInfo
 szTitle = "This is the title"
 With tBrowseInfo
  .hWndOwner = 0 'Me.hwnd
  .lpszTitle = lstrcat(szTitle, "")
  .ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN
 End With
 lpIDList = SHBrowseForFolder(tBrowseInfo)
 If (lpIDList) Then
  sBuffer = Space(MAX_PATH)
  SHGetPathFromIDList lpIDList, sBuffer
  sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
 End If
 
 GetFolderName = sBuffer
End Function

Download this snippet    Add to My Saved Code

Choose Directory Comments

No comments have been posted about Choose Directory. Why not be the first to post a comment about Choose Directory.

Post your comment

Subject:
Message:
0/1000 characters