首先将文件显示做了区分:文件夹名称显示folderlist(),文件名称显示filelist():
Private Function folderlist(pathname As String, treeflag As Integer) 'treeflag:0列表 1树形 2树形展开
'文件夹数和名称
Dim sf As String
sf = Dir(pathname, vbDirectory) '获取第一个目录名
foldernum = 0
Do While sf <> ""
' 跳过当前的目录及上层目录
If sf <> "." And sf <> ".." Then
If (GetAttr(pathname + sf) And vbDirectory) = vbDirectory Then
If treeflag = 0 Then
Text2 = Text2 & sf & vbCrLf ' 如果它是一个目录,将其名称添加到列表
ElseIf treeflag = 1 Then
Text2 = Text2 & Treestr & sf & vbCrLf
ElseIf treeflag = 2 Then '树形展开的时候,需要了解子目录,所以还需进行递归调用
Text2 = Text2 & Treestr & sf & vbCrLf
folderlist pathname + sf, treeflag
filelist pathname + sf, treeflag
End If
foldernum = foldernum + 1 '统计文件夹数目
End If
End If
sf = Dir() ' 查找下一个目录
Loop
If treeflag = 0 Then
If foldernum <> 0 Then
Text2 = Text2 & vbCrLf & "======共有" & foldernum & "个文件夹======" & vbCrLf & vbCrLf
Else
Text2 = Text2 & "======无子目录======" & vbCrLf & vbCrLf
End If
ElseIf foldernum <> 0 Then
Text2 = Text2 & Nullstr & vbCrLf & Nullstr & vbCrLf
End If
End Function
Private Function filelist(pathname As String, treeflag As Integer) '0列表 1树形 2树形展开
'文件数和名称
Dim a As String
a = Dir(pathname & "/*.*")
filenum = 0
Do While a <> ""
If treeflag = 0 Then
Text2 = Text2 & a & vbCrLf
Else
Text2 = Text2 & Treestr & a & vbCrLf
End If
filenum = filenum + 1 '统计文件数目
a = Dir
Loop
If treeflag = 0 Then
If filenum <> 0 Then
Text2 = Text2 & "======共有" & filenum & "个文件======"
Else
Text2 = Text2 & "======无文件信息======"
End If
End If
End Function
'显示所有文件到LIST列表
Public Sub getAllPic(ByVal strPath As String)
Try
'获取文件列表数组
Dim filename() As String
filename = System.IO.Directory.GetFiles(strPath)
'添加到LIST中
ListBox2.Items.Clear()
Dim i As Integer
For i = 0 To filename.Length - 1
ListBox2.Items.Add(filename(i))
Next i
Catch ex As Exception
MsgBox(ex.Message)
MsgBox("无文件!")
End Try
End Sub