Hello everyone,
Post by qfelI guess CreateFile is faster because for FindFirstFile you have to check
found file name (AFAIR FindFirstFile can return "file1234" for pattern
"file1").
Just pass all share access and no rights to CreateFile;
Also, you should check if file opening successes, file can get deleted after
you had checked for its existence, so what's the point?
Does anyone have reliable figures or the right answer to a similar
question: What is the fastest way to determine the size of a file via
GetFileSize? Passing a handle from FindFirstFile or passing a handle
from CreateFile? A peer of mine claims that passing a handle from
FindFirstFile was an order of magnitude faster in her tests than using
CreateFile. Does FindFirstFile call CreateFile under the hood or is a
completely different implementation employed for FindFirstFile instead?
The two implementations (for files<4GB) of hers are the following:
BOOL FileGetSize(LPCTSTR lpszFileName, DWORD& dwSize)
{
WIN32_FIND_DATA FD;
HANDLE hFindFile = FindFirstFile ( lpszFileName, &FD );
if (hFindFile == INVALID_HANDLE_VALUE)
return FALSE;
FindClose (hFindFile);
// dont return Directories
if (FD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
return FALSE;
dwSize = FD.nFileSizeLow;
return TRUE;
}
BOOL FileGetSize2(LPCTSTR lpszFileName, DWORD& dwSize)
{
HANDLE hFindFile = CreateFile(lpszFileName, GENERIC_READ,
FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if (hFindFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
dwSize = GetFileSize(hFindFile, 0);
CloseHandle(hFindFile);
return TRUE;
}
Comments anyone?
--
Stefan Kuhr
"Lesen schadet der Dummheit"