今天就跟大家聊聊有關(guān)VB.NET中怎么實現(xiàn) API函數(shù)遍歷,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、成都網(wǎng)站設(shè)計、姜堰網(wǎng)絡(luò)推廣、小程序制作、姜堰網(wǎng)絡(luò)營銷、姜堰企業(yè)策劃、姜堰品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供姜堰建站搭建服務(wù),24小時服務(wù)熱線:18982081108,官方網(wǎng)址:www.cdcxhl.com
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'關(guān)閉查找文件的API
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
'以下為調(diào)用瀏覽文件夾窗口的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 LongPrivate 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'常量
Const MAX_PATH = 260
Const MAXDWORD = &HFFFF
Const INVALID_HANDLE_VALUE = -1
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100
Const BIF_RETURNONLYFSDIRS = 1
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
'定義類(用于查找文件)
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
'定義類(用于瀏覽文件夾窗口)
Private Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
'自定義函數(shù)
Function StripNulls(OriginalStr As String) As String
If (InStr(OriginalStr, Chr(0)) > 0) Then
OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
End If
StripNulls = OriginalStr
End Function
'自定義函數(shù)
Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, _
DirCount As Integer)
Dim FileName As String ' 文件名
Dim DirName As String ' 子目錄名
Dim dirNames() As String ' 目錄數(shù)組
Dim nDir As Integer ' 當(dāng)前路徑的目錄數(shù)
Dim i As Integer ' 循環(huán)計數(shù)器變量
Dim hSearch As Long ' 搜索句柄變量
Dim WFD As WIN32_FIND_DATA
Dim Cont As Integer
If Right(path, 1) <> "\" Then pathpath = path & "\"
'搜索子目錄
nDir = 0
ReDim dirNames(nDir)
Cont = True
hSearch = FindFirstFile(path & "*", WFD)
If hSearch <> INVALID_HANDLE_VALUE Then
Do While Cont
DirName = StripNulls(WFD.cFileName)
If (DirName <> ".") And (DirName <> "..") Then
If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
dirNames(nDir) = DirName
DirCountDirCount = DirCount + 1
nDirnDir = nDir + 1
ReDim Preserve dirNames(nDir)
End If
End If
Cont = FindNextFile(hSearch, WFD) '獲取下一個子目錄
Loop
Cont = FindClose(hSearch)
End If
' 遍歷目錄并累計文件總數(shù)
hSearch = FindFirstFile(path & SearchStr, WFD)
Cont = True
If hSearch <> INVALID_HANDLE_VALUE Then
While Cont
FileName = StripNulls(WFD.cFileName)
If (FileName <> ".") And (FileName <> "..") Then
FindFilesAPIFindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
FileCountFileCount = FileCount + 1
List1.AddItem path & FileName
End If
Cont = FindNextFile(hSearch, WFD) ' 獲取下一個文件
Wend
Cont = FindClose(hSearch)
End If
'如果子目錄存在則遍歷之
If nDir > 0 Then
For i = 0 To nDir - 1
FindFilesAPIFindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", _
SearchStr, FileCount, DirCount)
Next i
End If
End Function
'查找按鈕代碼
Sub Command1_Click()
Dim SearchPath As String, FindStr As String
Dim FileSize As Long
Dim NumFiles As Integer, NumDirs As Integer
Dim iNull As Integer, lpIDList As Long, lResult As Long
Dim sPath As String, udtBI As BrowseInfo
With udtBI
'設(shè)置瀏覽窗口
.hWndOwner = Me.hWnd
'返回選中的目錄
.ulFlags = BIF_RETURNONLYFSDIRS
End With
'調(diào)出瀏覽窗口
lpIDList = SHBrowseForFolder(udtBI)
If lpIDList Then
sPath = String$(MAX_PATH, 0)
'獲取路徑
SHGetPathFromIDList lpIDList, sPath
'釋放內(nèi)存
CoTaskMemFree lpIDList
iNull = InStr(sPath, vbNullChar)
If iNull Then
sPath = Left$(sPath, iNull - 1)
End If
End If
Screen.MousePointer = vbHourglass
List1.Clear
SearchPath = sPath '選中的目錄為搜索的起始路徑
FindStr = "*.*" '搜索所有類型的文件(此處可另作定義)
FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
Text1.Text = "查找到的文件數(shù):" & NumFiles & vbCrLf & "查找的目錄數(shù):" & _
NumDirs + 1 & vbCrLf & "文件大小總共為:" & vbCrLf & _
Format(FileSize, "#,###,###,##0") & "字節(jié)"
Screen.MousePointer = vbDefault
End Sub
'調(diào)出瀏覽窗口
lpIDList = SHBrowseForFolder(udtBI)
If lpIDList Then
sPath = String$(MAX_PATH, 0)
'獲取路徑
SHGetPathFromIDList lpIDList, sPath
'釋放內(nèi)存
CoTaskMemFree lpIDList
iNull = InStr(sPath, vbNullChar)
If iNull Then
sPath = Left$(sPath, iNull - 1)
End If
End If
Screen.MousePointer = vbHourglass
List1.Clear
SearchPath = sPath '選中的目錄為搜索的起始路徑
FindStr = "*.*" '搜索所有類型的文件(此處可另作定義)
FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
Text1.Text = "查找到的文件數(shù):" & NumFiles & vbCrLf & "查找的目錄數(shù):" & _
NumDirs + 1 & vbCrLf & "文件大小總共為:" & vbCrLf & _
Format(FileSize, "#,###,###,##0") & "字節(jié)"
Screen.MousePointer = vbDefault
End Sub
看完上述內(nèi)容,你們對VB.NET中怎么實現(xiàn) API函數(shù)遍歷有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。