Edit, (see comments)
Using GetFileInformationByHandle
ULONGLONG filesize = 0;
HANDLE h = CreateFile(filename, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (h != INVALID_HANDLE_VALUE)
{
BY_HANDLE_FILE_INFORMATION info;
memset(&info, 0, sizeof(BY_HANDLE_FILE_INFORMATION));
if (GetFileInformationByHandle(h, &info))
{
ULARGE_INTEGER ul = { 0 };
ul.LowPart = info.nFileSizeLow;
ul.HighPart = info.nFileSizeHigh;
filesize = ul.QuadPart;
}
CloseHandle(h);
}
Another method, see GetFileAttributesEx
There is also FindFirstFile
, but this can be inaccurate
From MSDN documentation for FindFirstFile
Note In rare cases or on a heavily loaded system, file attribute
information on NTFS file systems may not be current at the time this
function is called. To be assured of getting the current NTFS file
system file attributes, call the GetFileInformationByHandle
function.
Using FindFirstFile
WIN32_FIND_DATA ffd;
HANDLE hfind = FindFirstFile(filename, &ffd);
if (hfind != INVALID_HANDLE_VALUE)
{
DWORD filesize = ffd.nFileSizeLow;
//for files larger than 4GB:
ULARGE_INTEGER ul;
ul.LowPart = ffd.nFileSizeLow;
ul.HighPart = ffd.nFileSizeHigh;
ULONGLONG llfilesize = ul.QuadPart;
FindClose(hfind);
}