轉貼自:
http://members.lycos.co.uk/nirsoft/vc/isfileexist.html
一共有三個方法, 請參考:
IsFileExist1: We try to open the file, if we get a valid hanle, the file exists.
IsFileExist2: We try to find the file by using the FindFirstFile API function. if we get a valid hanle, the file exists.
IsFileExist3: We try to get the attributes of the file. If the function fails, and returns 0xffffffff value, the file doesn't exist.
BOOL IsFileExist1(LPSTR lpszFilename)
{
HANDLE hFile = CreateFile(lpszFilename, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hFile != INVALID_HANDLE_value)
{
CloseHandle(hFile);
return TRUE;
} else
{
return FALSE;
}
}
BOOL IsFileExist2(LPSTR lpszFilename)
{
WIN32_FIND_DATA wfd;
HANDLE hFind = FindFirstFile(lpszFilename, &wfd);
if (hFind == INVALID_HANDLE_value)
return FALSE;
else
{
FindClose(hFind);
return TRUE;
}
}
BOOL IsFileExist3(LPSTR lpszFilename)
{
DWORD dwAttr = GetFileAttributes(lpszFilename);
if (dwAttr == 0xffffffff)
return FALSE;
else
return TRUE;
}