VB.NET中怎么实现 API函数遍历

发布时间:2021-08-13 13:52:43 作者:Leah
来源:亿速云 阅读:141

今天就跟大家聊聊有关VB.NET中怎么实现 API函数遍历,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

Option Explicit

  1. '查找***个文件的API  

  2. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" 
    (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long  

  3. '查找下一个文件的API  

  4. Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" 
    (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long  

  5. '获取文件属性的API  

  6. Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" 
    (ByVal lpFileName As String) As Long  

  7. '关闭查找文件的API  

  8. Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long  

  9. '以下为调用浏览文件夹窗口的API  

  10. Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)  

  11. Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" 
    (ByVal lpString1 As String, ByVal lpString2 As String) As Long  

  12. Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long  

  13. Private Declare Function SHGetPathFromIDList Lib "shell32" 
    (ByVal pidList As Long, ByVal lpBuffer As String) As Long  

  14. '常量  

  15. Const MAX_PATH = 260 

  16. Const MAXDWORD = &HFFFF  

  17. Const INVALID_HANDLE_VALUE = -1  

  18. Const FILE_ATTRIBUTE_ARCHIVE = &H20  

  19. Const FILE_ATTRIBUTE_DIRECTORY = &H10  

  20. Const FILE_ATTRIBUTE_HIDDEN = &H2  

  21. Const FILE_ATTRIBUTE_NORMAL = &H80  

  22. Const FILE_ATTRIBUTE_READONLY = &H1  

  23. Const FILE_ATTRIBUTE_SYSTEM = &H4  

  24. Const FILE_ATTRIBUTE_TEMPORARY = &H100  

  25. Const BIF_RETURNONLYFSDIRS = 1 

  26. Private Type FILETIME  

  27. dwLowDateTime As Long  

  28. dwHighDateTime As Long  

  29. End Type  

  30. '定义类(用于查找文件)  

  31. Private Type WIN32_FIND_DATA  

  32. dwFileAttributes As Long  

  33. ftCreationTime As FILETIME  

  34. ftLastAccessTime As FILETIME  

  35. ftLastWriteTime As FILETIME  

  36. nFileSizeHigh As Long  

  37. nFileSizeLow As Long  

  38. dwReserved0 As Long  

  39. dwReserved1 As Long  

  40. cFileName As String * MAX_PATH  

  41. cAlternate As String * 14  

  42. End Type  

  43. '定义类(用于浏览文件夹窗口)  

  44. Private Type BrowseInfo  

  45. hWndOwner As Long  

  46. pIDLRoot As Long  

  47. pszDisplayName As Long  

  48. lpszTitle As Long  

  49. ulFlags As Long  

  50. lpfnCallback As Long  

  51. lParam As Long  

  52. iImage As Long  

  53. End Type  

  54. '自定义函数  

  55. Function StripNulls(OriginalStr As String) As String  

  56. If (InStr(OriginalStr, Chr(0)) > 0) Then  

  57. OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)  

  58. End If  

  59. StripNulls = OriginalStr 

  60. End Function  

  61. '自定义函数  

  62. Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, _  

  63. DirCount As Integer)  

  64. Dim FileName As String ' 文件名  

  65. Dim DirName As String ' 子目录名  

  66. Dim dirNames() As String ' 目录数组  

  67. Dim nDir As Integer ' 当前路径的目录数  

  68. Dim i As Integer ' 循环计数器变量  

  69. Dim hSearch As Long ' 搜索句柄变量  

  70. Dim WFD As WIN32_FIND_DATA  

  71. Dim Cont As Integer  

  72. If Right(path, 1) <> "\" Then pathpath = path & "\"  

  73. '搜索子目录  

  74. nDir = 0 

  75. ReDim dirNames(nDir)  

  76. Cont = True 

  77. hSearch = FindFirstFile(path & "*", WFD)  

  78. If hSearch <> INVALID_HANDLE_VALUE Then  

  79. Do While Cont  

  80. DirName = StripNulls(WFD.cFileName)  

  81. If (DirName <> ".") And (DirName <> "..") Then  

  82. If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then  

  83. dirNames(nDir) = DirName  

  84. DirCountDirCount = DirCount + 1  

  85. nDirnDir = nDir + 1  

  86. ReDim Preserve dirNames(nDir)  

  87. End If  

  88. End If  

  89. Cont = FindNextFile(hSearch, WFD) '获取下一个子目录  

  90. Loop  

  91. Cont = FindClose(hSearch)  

  92. End If  

  93. ' 遍历目录并累计文件总数  

  94. hSearch = FindFirstFile(path & SearchStr, WFD)  

  95. Cont = True 

  96. If hSearch <> INVALID_HANDLE_VALUE Then  

  97. While Cont  

  98. FileName = StripNulls(WFD.cFileName)  

  99. If (FileName <> ".") And (FileName <> "..") Then  

  100. FindFilesAPIFindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow  

  101. FileCountFileCount = FileCount + 1  

  102. List1.AddItem path & FileName  

  103. End If  

  104. Cont = FindNextFile(hSearch, WFD) ' 获取下一个文件  

  105. Wend  

  106. Cont = FindClose(hSearch)  

  107. End If  

  108. '如果子目录存在则遍历之  

  109. If nDir > 0 Then  

  110. For i = 0 To nDir - 1  

  111. FindFilesAPIFindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", _  

  112. SearchStr, FileCount, DirCount)  

  113. Next i  

  114. End If  

  115. End Function  

  116. '查找按钮代码  

  117. Sub Command1_Click()  

  118. Dim SearchPath As String, FindStr As String  

  119. Dim FileSize As Long  

  120. Dim NumFiles As Integer, NumDirs As Integer  

  121. Dim iNull As Integer, lpIDList As Long, lResult As Long  

  122. Dim sPath As String, udtBI As BrowseInfo  

  123. With udtBI  

  124. '设置浏览窗口  

  125. .hWndOwner = Me.hWnd  

  126. '返回选中的目录  

  127. .ulFlags = BIF_RETURNONLYFSDIRS 

  128. End With  

  129. '调出浏览窗口  

  130. lpIDList = SHBrowseForFolder(udtBI)  

  131. If lpIDList Then  

  132. sPath = String$(MAX_PATH, 0)  

  133. '获取路径  

  134. SHGetPathFromIDList lpIDList, sPath  

  135. '释放内存  

  136. CoTaskMemFree lpIDList  

  137. iNull = InStr(sPath, vbNullChar)  

  138. If iNull Then  

  139. sPath = Left$(sPath, iNull - 1)  

  140. End If  

  141. End If  

  142. Screen.MousePointer = vbHourglass 

  143. List1.Clear  

  144. SearchPath = sPath '选中的目录为搜索的起始路径  

  145. FindStr = "*.*" '搜索所有类型的文件(此处可另作定义)  

  146. FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)  

  147. Text1.Text = "查找到的文件数:" & NumFiles & vbCrLf & "查找的目录数:" & _  

  148. NumDirs + 1 & vbCrLf & "文件大小总共为:" & vbCrLf & _  

  149. Format(FileSize, "#,###,###,##0") & "字节"  

  150. Screen.MousePointer = vbDefault 

  151. End Sub   

  152. '调出浏览窗口  

  153. lpIDList = SHBrowseForFolder(udtBI)  

  154. If lpIDList Then  

  155. sPath = String$(MAX_PATH, 0)  

  156. '获取路径  

  157. SHGetPathFromIDList lpIDList, sPath  

  158. '释放内存  

  159. CoTaskMemFree lpIDList  

  160. iNull = InStr(sPath, vbNullChar)  

  161. If iNull Then  

  162. sPath = Left$(sPath, iNull - 1)  

  163. End If  

  164. End If  

  165. Screen.MousePointer = vbHourglass 

  166. List1.Clear  

  167. SearchPath = sPath '选中的目录为搜索的起始路径  

  168. FindStr = "*.*" '搜索所有类型的文件(此处可另作定义)  

  169. FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)  

  170. Text1.Text = "查找到的文件数:" & NumFiles & vbCrLf & "查找的目录数:" & _  

  171. NumDirs + 1 & vbCrLf & "文件大小总共为:" & vbCrLf & _  

  172. Format(FileSize, "#,###,###,##0") & "字节"  

  173. Screen.MousePointer = vbDefault 

  174. End Sub    

看完上述内容,你们对VB.NET中怎么实现 API函数遍历有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. VB.NET中怎么修改系统时间
  2. VB.NET中怎么打开Notes数据库

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vb.net

上一篇:JSP中怎么利用JDBC连接MySQL

下一篇:Python中怎么保存搜索引擎结果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》