I need a function or command in Delphi
to return the mapping letter of the user's drivers.
example: Z:
, U:
I need a function or command in Delphi
to return the mapping letter of the user's drivers.
example: Z:
, U:
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils, Windows, Types;
// Returns an array filled wit the assigned
// drive letters on the current computer.
function GetDriveList: TStringDynArray;
var
Buff: array[0..128] of Char;
ptr: PChar;
Idx: Integer;
begin
if (GetLogicalDriveStrings(Length(Buff), Buff) = 0) then
RaiseLastOSError;
// There can't be more than 26 lettered drives (A..Z).
SetLength(Result, 26);
Idx := 0;
ptr := @Buff;
while StrLen(ptr) > 0 do
begin
Result[Idx] := ptr;
ptr := StrEnd(ptr);
Inc(ptr);
Inc(Idx);
end;
SetLength(Result, Idx);
end;
// Converts a drive letter into the integer drive #
// required by DiskSize().
function DOSDrive( const sDrive: String ): Integer;
begin
if (Length(sDrive) < 1) then
Result := -1
else
Result := (Ord(UpCase(sDrive[1])) - 64);
end;
// Tests the status of a drive to see if it's ready
// to access.
function DriveReady(const sDrive: String): Boolean;
var
ErrMode: Word;
begin
ErrMode := SetErrorMode(0);
SetErrorMode(ErrMode or SEM_FAILCRITICALERRORS);
try
Result := (DiskSize(DOSDrive(sDrive)) > -1);
finally
SetErrorMode(ErrMode);
end;
end;
// Demonstrates using the above functions.
var
DrivesArray: TStringDynArray;
Drive: string;
const
StatusStr = 'Drive %s is ready: %s';
begin
DrivesArray := GetDriveList;
for Drive in DrivesArray do
WriteLn(Format(StatusStr, [Drive, BoolToStr(DriveReady(Drive), True)]));
ReadLn;
end.
Output:
Drive C:\ is ready: True
Drive D:\ is ready: True