打印本文 打印本文 关闭窗口 关闭窗口
用API函数遍历指定驱动器、目录的文件
作者:武汉SEO闵涛  文章来源:敏韬网  点击数827  更新时间:2009/4/23 14:59:16  文章录入:mintao  责任编辑:mintao
  以下代码演示了如何用Windows API函数遍历指定驱动器、目录的所有文件。其思路是:调出浏览文件夹窗口让用户指定所要搜索的起始路径,然后用查找文件的API函数遍历该目录下及其包含的子目录下的所有文件。本例需要:一个按钮,一个TextBox和一个ListBox,其中,TextBox应设置为多行。
核心代码参照API-Guide的两个例子程序,特此声明。

Option Explicit

  查找第一个文件的API

Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
  查找下一个文件的API

Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
  获取文件属性的API

Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
  关闭查找文件的API

Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
  以下为调用浏览文件夹窗口的API

Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
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

打印本文 打印本文 关闭窗口 关闭窗口