Here's the FileSize() method in my XP library, which of course handles files >2gig. (As do all the drive size methods, obviously.) I'm not going to bother adding in hungarian notation, which I do not use in the XP library to make the interface more intuitive.This is about as fast and easy as it gets.vb Code:
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long ' Currency handles file sizes over 2 gig ' Thanks to Richard Newcombe from codeguru.com Public Function GetSize(ByVal File As String) As Currency Const GENERIC_READ = &H80000000 Const FILE_SHARE_READ = &H1 Const FILE_SHARE_WRITE = &H2 Const OPEN_EXISTING = 3 Dim lngHandle As Long Dim lngLow As Long Dim lngHigh As Long Dim curFileSize As Currency ' Open the file lngHandle = CreateFile(File, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0, 0) ' Get the file size lngLow = GetFileSize(lngHandle, lngHigh) CloseHandle lngHandle ' Combine the Low and High values into one currency ' Must use the '@' currency declaration or IDE will balk curFileSize = 4294967295@ * lngHigh If lngLow < 0 Then curFileSize = curFileSize + (4294967295@ + (lngLow + 1)) Else curFileSize = curFileSize + lngLow End If GetSize = curFileSize End Function




Reply With Quote