unit DriveUtils;
interface
uses
System.SysUtils,
WinAPI.Windows,
Winapi.ShellAPI;
type
TDiskSign = string[2];
TDiskType = (dtUnknown, dtNotExists, dtRemovable, dtFixed, dtRemote, dtCDROM, dtRAMDisk, dtRemovableNotConnected);
TDiskInfo = record
DiskSign : string;
DiskType : TDiskType;
VolumeLabel : string;
ImageIndex : integer;
end;
function DiskTypeToStr (aDiskType: TDiskType) : string;
function GetDiskInfo (aDiskSign : String) : TDiskInfo;
implementation
function DiskTypeToStr (aDiskType: TDiskType) : string;
begin
case aDiskType of
dtUnknown : result := 'Unknown';
dtNotExists : result := 'Does''t exists';
dtRemovable : result := 'Removable';
dtFixed : result := 'Fixed';
dtRemote : result := 'Remote';
dtCDROM : result := 'CDrom';
dtRAMDisk : result := 'RAMdisk';
dtRemovableNotConnected : result := 'Removable disconnected';
end;
end;
function GetDiskInfo (aDiskSign : String) : TDiskInfo;
var
lpnLength : Cardinal;
lError : DWORD;
lBufVolumeLabel : TArray<Char>;
begin
Result.DiskSign := aDiskSign; // TDiskInfo.Sign = Driveletter STRING[2]
Result.DiskType := dtUnknown; // Init
Result.VolumeLabel := ''; // Init
Result.ImageIndex := -1; // Init
case GetDriveType(PChar(IncludeTrailingPathDelimiter(aDiskSign))) of // Check if drive removable, fixed, CD-ROM, RAM disk, or network drive.
DRIVE_UNKNOWN : Result.DiskType := dtUnknown; // The drive type cannot be determined
DRIVE_NO_ROOT_DIR : Result.DiskType := dtNotExists; // The root path is invalid. For example, no volume is mounted at the path.
DRIVE_REMOVABLE : Result.DiskType := dtRemovable; // The disk can be removed from the drive.
DRIVE_FIXED : Result.DiskType := dtFixed; // The disk cannot be removed from the drive
DRIVE_REMOTE : Result.DiskType := dtRemote; // The drive is a remote (network) drive
DRIVE_CDROM : Result.DiskType := dtCDROM; // The drive is a CD-ROM drive
DRIVE_RAMDISK : Result.DiskType := dtRAMDisk; // The drive is a RAM disk
end;
if Result.DiskType = dtNotExists then // if NotExists then start checking for Disconnected drive
begin
lpnLength := 0; // Init
lError := WNetGetConnection(PChar(aDiskSign), nil, lpnLength); // Get Size for lBufVolumeLabel in lpnLength
if lError = ERROR_MORE_DATA then // size received in lpnLength
begin
SetLength(lBufVolumeLabel, lpnLength);
lError := WNetGetConnection(PChar(aDiskSign), PChar(lBufVolumeLabel), lpnLength); // Get network name
if lError = ERROR_CONNECTION_UNAVAIL then // It exists but is unavailable
begin
SetString(Result.VolumeLabel, PChar(lBufVolumeLabel), lpnLength - 1); // Return volume label, length includes terminating #0 char, we don't need that
Result.DiskType := dtRemovableNotConnected; // Return DiskType
Result.ImageIndex := SIID_DRIVENETDISABLED; // Set imageindex to disconnected icon
end
else
Result.DiskType := dtNotExists; // otherwise reurn dtNotExists
end;
end;
end;
end.
Bookmarks