判断指定路径是否为一个存在的文件的方法
#include <windows.h>
bool is_existing_file(const wchar_t *path)
{
HANDLE hfile(NULL);
::SetLastError(ERROR_SUCCESS);
hfile = ::CreateFileW
(
path,
FILE_READ_EA,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL
);
DWORD error = ::GetLastError();
#ifdef DEBUG
printf("hfile: %p\n", hfile);
printf("lastError: %lu\n", error);
#endif
if (hfile == NULL || hfile == INVALID_HANDLE_VALUE)
{
return error != ERROR_PATH_NOT_FOUND &&
error != ERROR_FILE_NOT_FOUND;
}
else
{
::CloseHandle(hfile);
hfile = NULL;
return true;
}
}