'一个窗体,一个list,一启动就会自动把给定的目录里所有的jpg文件读出,并添加到list中。by fallx
Private Sub Form_Load()
On Error GoTo errEvent
Dim strFileName() As String
Dim strPath As String
Dim intCount, i As Long
Dim strFile As String
intCount = 0
strPath = "D:\相片\" & "*.jpg" '这个目录可以改为用 打开文件夹给出,*.jpg限定文件类型
strFile = Dir(strPath)
While strFile <> "" '搜索给定目录下的所有该类型文件
DoEvents
intCount = intCount + 1
ReDim Preserve strFileName(intCount) As String '重定义数组
strFileName(intCount - 1) = strFile '把文件名存到数组中
strFile = Dir '再取下一个
Wend
For i = 0 To intCount - 1
List1.AddItem strFileName(i) & ".jpg" '将文件名插入listbox的里面
Next i
Exit Sub
errEvent:
MsgBox "非常抱歉,出错,部分或全部图片无法读取!"
End Sub