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开发者_Go百科 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;
}
Use GetFileAttributes function.
精彩评论