简体   繁体   中英

How to check whether a folder or a file is hidden=

I want to find out whether a file or a directory is hide.

At first I made use of CFile::GetStatus(), however I found this api sometimes will return FALSE.

I don't know why, So I wrote the following code, However I found it is not stable. What is wrong with my code?

  BOOL IsHide(const CString& strPath, BOOL& bIsHide)
  {
   if (strPath.GetLength() <= 3)
   {
    bIsHide = FALSE;
    return  TRUE;
   }
   bIsHide = FALSE;
   HANDLE hFile = CreateFile( strPath, 0, FILE_SHARE_READ,
    NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS,
    NULL);
   if (hFile == INVALID_HANDLE_VALUE)
   {
    ASSERT(FALSE);
    return FALSE;
   }
   BY_HANDLE_FILE_INFORMATION fiBuf;  
   GetFileInformationByHandle( hFile, &fiBuf );
   CloseHandle(hFile);
   WORD isHide = (fiBuf.dwFileAttributes) | FILE_ATTRIBUTE_HIDDEN;
   if (isHide == fiBuf.dwFileAttributes)
   {
    bIsHide = TRUE;
   }
   else
   {
    bIsHide = FALSE;
   }
   return TRUE;
}

使用GetFileAttributes函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM