Пример.
Это должно быть включено в функцию потока, которая запускает эту функцию через интервал времени (1 секунда).Функция потока должна использовать это и отправлять в приложение уведомление об изменении драйвера.
ПРЕДУПРЕЖДЕНИЕ: УЖАСНЫЙ КОД.ПОЖАЛУЙСТА, ИСПОЛЬЗУЙТЕ ЭТО В КАЧЕСТВЕ ПРИМЕРА И УЛУЧШИТЕ ЭТО, КАК ВЫ УВИДЕТЕ.
BOOL CheckDirProperties(const CString& path, BOOL& bReadOnly, BOOL& bRemovable)
{
DWORD FileAttributes;
DWORD DriveAttributes;
UINT uDriveType;
if( path.GetLength() < 2 ||path.GetAt( 1 ) != ':' )
{
// invalid path, abort
return FALSE;
}
//Ugly path handling
CString szFormattedDrivePath("C:\\"); // string of length 3 where drive letter will be replaced
// Replace the drive letter with the drive letter from the path
szFormattedDrivePath.SetAt( 0, path.GetAt( 0 ) );
DriveAttributes = GetFileAttributes( szFormattedDrivePath );
FileAttributes = GetFileAttributes( path);
uDriveType = GetDriveType( szFormattedDrivePath );
if( !(FileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
!(DriveAttributes & FILE_ATTRIBUTE_DIRECTORY) )
{ // Not a directory
return FALSE;
}
if( (FileAttributes & FILE_ATTRIBUTE_ARCHIVE) ||
(DriveAttributes & FILE_ATTRIBUTE_ARCHIVE) ||
(FileAttributes & FILE_ATTRIBUTE_ENCRYPTED) ||
(DriveAttributes & FILE_ATTRIBUTE_ENCRYPTED) ||
(FileAttributes & FILE_ATTRIBUTE_OFFLINE) ||
(DriveAttributes & FILE_ATTRIBUTE_OFFLINE) ||
(FileAttributes & FILE_ATTRIBUTE_OFFLINE) ||
(DriveAttributes & FILE_ATTRIBUTE_OFFLINE) )
{ // Not a directory
TRACE("The directory %s on drive %s has unexpected file attributes. Problems may occur.\n",path, szFormattedDrivePath );
}
// To set m_bReadOnly to true, we need to know that the entire subtree is readonly.
// Even if the drive or the directory has the FILE_ATTRIBUTE_READONLY set, the content may not be read-only.
// Therefore the default value of bReadOnly must be FALSE.
bReadOnly = FALSE;
switch( uDriveType )
{
case DRIVE_FIXED:
case DRIVE_REMOTE:
bRemovable = FALSE;
break;
case DRIVE_CDROM:
bRemovable = TRUE;
bReadOnly = TRUE; // We know that a CD-ROM drive is always read-only
break;
case DRIVE_REMOVABLE:
case DRIVE_RAMDISK:
bRemovable = TRUE;
break;
case DRIVE_NO_ROOT_DIR: // fall through
case DRIVE_UNKNOWN: // fall through
default:
bRemovable = TRUE; // assume it is removable if we don't know what value to set
break;
}
return TRUE;
}