How can we check if a file Exists or not using Win32 program?

Krishnan picture Krishnan · Sep 30, 2010 · Viewed 79k times · Source

How can we check if a file Exists or not using a Win32 program? I am working for a Windows Mobile App.

Answer

Zach Burlingame picture Zach Burlingame · Jun 2, 2011

Use GetFileAttributes to check that the file system object exists and that it is not a directory.

BOOL FileExists(LPCTSTR szPath)
{
  DWORD dwAttrib = GetFileAttributes(szPath);

  return (dwAttrib != INVALID_FILE_ATTRIBUTES && 
         !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}

Copied from How do you check if a directory exists on Windows in C?