Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a file size even if it is already open for exclusive access?

Tags:

c

winapi

The only way I found to get a file size is to use the GetFileSizeEx() function. However, this function requires a HANDLE to the file, and if the file is already open for exclusive access I will not be able to get a HANDLE for it and hence I will not be able to get its size.

So is there a way to get a file size even if it is already open for exclusive access?

like image 659
John Avatar asked Dec 28 '25 23:12

John


1 Answers

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);
}
like image 121
Barmak Shemirani Avatar answered Dec 30 '25 15:12

Barmak Shemirani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!