溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

VB.NET中怎么實現(xiàn) API函數(shù)遍歷

發(fā)布時間:2021-08-13 13:52:43 來源:億速云 閱讀:159 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關(guān)VB.NET中怎么實現(xiàn) API函數(shù)遍歷,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

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. '關(guān)閉查找文件的API  

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

  9. '以下為調(diào)用瀏覽文件夾窗口的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. '自定義函數(shù)  

  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. '自定義函數(shù)  

  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 ' 目錄數(shù)組  

  67. Dim nDir As Integer ' 當前路徑的目錄數(shù)  

  68. Dim i As Integer ' 循環(huán)計數(shù)器變量  

  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. ' 遍歷目錄并累計文件總數(shù)  

  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. '設(shè)置瀏覽窗口  

  125. .hWndOwner = Me.hWnd  

  126. '返回選中的目錄  

  127. .ulFlags = BIF_RETURNONLYFSDIRS 

  128. End With  

  129. '調(diào)出瀏覽窗口  

  130. lpIDList = SHBrowseForFolder(udtBI)  

  131. If lpIDList Then  

  132. sPath = String$(MAX_PATH, 0)  

  133. '獲取路徑  

  134. SHGetPathFromIDList lpIDList, sPath  

  135. '釋放內(nèi)存  

  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 = "查找到的文件數(shù):" & NumFiles & vbCrLf & "查找的目錄數(shù):" & _  

  148. NumDirs + 1 & vbCrLf & "文件大小總共為:" & vbCrLf & _  

  149. Format(FileSize, "#,###,###,##0") & "字節(jié)"  

  150. Screen.MousePointer = vbDefault 

  151. End Sub   

  152. '調(diào)出瀏覽窗口  

  153. lpIDList = SHBrowseForFolder(udtBI)  

  154. If lpIDList Then  

  155. sPath = String$(MAX_PATH, 0)  

  156. '獲取路徑  

  157. SHGetPathFromIDList lpIDList, sPath  

  158. '釋放內(nèi)存  

  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 = "查找到的文件數(shù):" & NumFiles & vbCrLf & "查找的目錄數(shù):" & _  

  171. NumDirs + 1 & vbCrLf & "文件大小總共為:" & vbCrLf & _  

  172. Format(FileSize, "#,###,###,##0") & "字節(jié)"  

  173. Screen.MousePointer = vbDefault 

  174. End Sub    

看完上述內(nèi)容,你們對VB.NET中怎么實現(xiàn) API函數(shù)遍歷有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI