Option Explicit
Private Const MAX_PATH = 260
Private Const BIF_RETURNONLYFSDIRS = 1
'With New Folder
'Private Const BIF_NEWDIALOGSTYLE As Long = &H40
'udtBI.ulFlags = BIF_RETURNONLYFSDIRS + BIF_NEWDIALOGSTYLE
'Browse the Share
'Private Const BIF_DONTGOBELOWDOMAIN = 2
'udtBI.ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN + BIF_NEWDIALOGSTYLE
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
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
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 Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Function BrowseFolder(hwndOwner As Long, sPrompt As String) As String
Dim iNull As Integer
Dim lpIDList As Long
Dim lResult As Long
Dim sPath As String
Dim udtBI As BrowseInfo
udtBI.hwndOwner = hwndOwner
udtBI.lpszTitle = lstrcat(sPrompt, "")
udtBI.ulFlags = BIF_RETURNONLYFSDIRS
lpIDList = SHBrowseForFolder(udtBI)
If lpIDList Then
sPath = String$(MAX_PATH, 0)
lResult = SHGetPathFromIDList(lpIDList, sPath)
Call CoTaskMemFree(lpIDList)
iNull = InStr(sPath, vbNullChar)
If iNull Then sPath = Left$(sPath, iNull - 1)
End If
BrowseFolder = sPath
End Function
Private Sub Form_Load()
Call BrowseFolder(Form1.hWnd, "请选择目标文件夹:")
End Sub