vb.net用api搜索文件却找不到任何文件,问题出在哪里?

清风不拂面 2020-08-22 11:14:34
Public Structure Win32FindData
Dim dwFileAttributes As Long '文件属性
Dim ftCreationTime As ComTypes.FILETIME '文件创建时间
Dim ftLastAccessTime As ComTypes.FILETIME '文件最后一次访问时间
Dim ftLastWriteTime As ComTypes.FILETIME '文件最后一次修改时间
Dim nFileSizeHigh As Long '文件长度高32位
Dim nFileSizeLow As Long '文件长度低32位
Dim dwReserved0 As Long '系统保留
Dim dwReserved1 As Long '系统保留
Dim cFileName As String ' *MAX_PATH'长文件名
Dim cAlternate As String '*14'8.3格式文件名
End Structure

<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function FindFirstFileA(ByVal pFileName As String, ByRef pFindFileData As Win32FindData) As IntPtr
End Function

'<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function FindNextFileA(ByRef hndFindFile As IntPtr, ByRef pFindFileData As Win32FindData) As IntPtr
End Function
'<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function FindClose(ByRef hndFindFile As IntPtr) As IntPtr
End Function


Public Sub getFilesInDirsApi(ByVal strSearchPath As String, ByVal strSearchExt As String, ByVal strSearchKey As String)

Dim wfdFiles As New Win32FindData
Dim inpSearch As IntPtr '搜索句柄变量
Dim INVALID_HANDLE_VALUE As IntPtr = New IntPtr(-1)
Dim listResult As New List(Of String)

'查找第一个文件
inpSearch = FindFirstFileA(strSearchPath & "*.*", wfdFiles)
If inpSearch <> INVALID_HANDLE_VALUE Then

If wfdFiles.dwFileAttributes <> FileAttributes.Directory Then '不是目录
strFileName = wfdFiles.cFileName
strFileExt = Strings.Right(strFileName, strFileName.Length - Strings.InStrRev(strFileName, ".") + 1)
If strFileExt <> "" AndAlso strSearchExt.Contains(strFileExt) Then
listResult.Add(strSearchPath & strFileName)
End If
Else
getFilesInDirsApi(strSearchPath, strSearchExt, strSearchKey)
End If
'查找后面的文件
While FindNextFileA(inpSearch, wfdFiles)
If wfdFiles.dwFileAttributes <> FileAttributes.Directory Then '不是目录
strFileName = wfdFiles.cFileName
strFileExt = Strings.Right(strFileName, strFileName.Length - Strings.InStrRev(strFileName, ".") + 1)
If strFileExt <> "" AndAlso strSearchExt.Contains(strFileExt) Then
listResult.Add(strSearchPath & strFileName)
End If
Else
getFilesInDirsApi(strSearchPath, strSearchExt, strSearchKey)
End If
End While
end sub
...全文
12739 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
清风不拂面 2020-08-25
  • 打赏
  • 举报
回复
没找到FindFirstFileW的数据类型,用了原来FindFirstFileA的类型,同样的问题。用了win10+64位,好象很多API不能用了。
八爻老骥 2020-08-23
  • 打赏
  • 举报
回复
用FindFirstFileW试试。
清风不拂面 2020-08-23
  • 打赏
  • 举报
回复
根据测试,不能查找根目录,只能查找程序所在当前目录,查msdn明明可以的,难道c语言可以,到vb这里就不行了? 测试了c:\*.*,c:\\*.*,c:\*,c:\\*都不行,SetCurrentDirectory(strpath)也不成功,怎么这么坑啊? 测试*.*得到的文件结果是.,继续下一文件就直接结束了 测试.\\*.*,终于得到一个目录,继续下一文件又结束了 晕死了
无语无 2020-08-23
  • 打赏
  • 举报
回复
用其他的试试

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧