|
- #If VBA7 Then
- Private Declare PtrSafe Function GetFileSizeEx Lib "kernel32" (ByVal hFile As LongPtr, ByRef FileSize As LARGE_INTEGER) As Long
- Private Declare PtrSafe Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, _
- lpSecurityAttributes As LongPtr, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As LongPtr) As LongPtr
- Private Declare PtrSafe Function CloseHandle Lib "kernel32" (ByVal hObject As LongPtr) As Long
- Dim lngHandle As LongPtr
- #Else
- Private Declare Function GetFileSizeEx Lib "kernel32" (ByVal hFile As Long, FileSize As LARGE_INTEGER ) As Long
- Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, _
- lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
- Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
- Dim lngHandle As Long
- #End If
- Private Const GENERIC_WRITE = &H40000000
- Private Const OPEN_EXISTING = 3
- Private Const FILE_SHARE_READ = &H1
- Private Const FILE_SHARE_WRITE = &H2
- Private Type LARGE_INTEGER
- LowPart As Long
- HighPart As Long
- End Type
- Function GetFileSize(sFilename As String) As Double
- Dim liFileSize As LARGE_INTEGER, lResult As Long
- lngHandle = CreateFile(sFilename, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
- lResult = GetFileSizeEx(lngHandle, liFileSize)
- CloseHandle lngHandle
- Dim a As Double
- a = 2 ^ 32
- If lResult <> 0 Then
- If liFileSize.LowPart >= 0 Then
- GetFileSize = liFileSize.HighPart * a + liFileSize.LowPart
- Else
- GetFileSize = (liFileSize.HighPart + 1) * a + liFileSize.LowPart
- End If
- End If
- End Function
- '用法演示
- Sub test()
- MsgBox GetFileSize("E:\JZ_WIN10_X64_V2022.07.iso")
- End Sub
复制代码
|
|