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 TypePrivate Sub Command1_Click() 'Opens a Treeview control that displays the directories in a computer '打开一个显示计算机内目录结构的Treeview控件 Dim lpIDList As Long Dim sBuffer As String Dim szTitle As String Dim tBrowseInfo As BrowseInfo szTitle = "请选择文件夹的位置:" With tBrowseInfo .hWndOwner = 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) Me.Caption = sBuffer End If End Sub
'递归遍历目录树 '参数: strPath 树的起始点路径 右边必须以"\"结束 Private Function EnumDirectoryTree(ByVal strPath As String) As Boolean Dim s As String Dim colTmp As New Collection Dim i As Long
Debug.Print strPath
s = Dir(strPath, vbDirectory)
'遍历此路径下所有目录及文件 Do While Len(s) If Left$(s, 1) <> "." Then '如果是文件夹名,就记下来 If GetAttr(strPath & s) = vbDirectory Then colTmp.Add strPath & s & "\" End If End If s = Dir(, vbDirectory) Loop '递归 For i = 1 To colTmp.Count EnumDirectoryTree colTmp.Item(i) Next
Set colTmp = Nothing End FunctionPrivate Sub Command1_Click() Call EnumDirectoryTree("c:\") End Sub
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 TypePrivate Sub Command1_Click()
'Opens a Treeview control that displays the directories in a computer
'打开一个显示计算机内目录结构的Treeview控件
Dim lpIDList As Long
Dim sBuffer As String
Dim szTitle As String
Dim tBrowseInfo As BrowseInfo
szTitle = "请选择文件夹的位置:"
With tBrowseInfo
.hWndOwner = 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)
Me.Caption = sBuffer
End If
End Sub
http://www.mndsoft.com/blog/article.asp?id=1008
'参数: strPath 树的起始点路径 右边必须以"\"结束
Private Function EnumDirectoryTree(ByVal strPath As String) As Boolean
Dim s As String
Dim colTmp As New Collection
Dim i As Long
Debug.Print strPath
s = Dir(strPath, vbDirectory)
'遍历此路径下所有目录及文件
Do While Len(s)
If Left$(s, 1) <> "." Then
'如果是文件夹名,就记下来
If GetAttr(strPath & s) = vbDirectory Then
colTmp.Add strPath & s & "\"
End If
End If
s = Dir(, vbDirectory)
Loop
'递归
For i = 1 To colTmp.Count
EnumDirectoryTree colTmp.Item(i)
Next
Set colTmp = Nothing
End FunctionPrivate Sub Command1_Click()
Call EnumDirectoryTree("c:\")
End Sub