Delphi - List drive and other drives on a Windows PC

Possible duplicate:
Get driver information (free space, etc.) for Windows drives and fill in notepad

I am new to programming (especially Delphi) and could not find examples of how to list all drives on a PC.

I really like hard drives and CD-Roms, but I could not find anything useful.

Can someone point me in the direction of a good working sample?

+5
source share
2 answers

The easiest way is to use GetDiskFreeSpaceExfrom a file sysutils.pas.

There are two parts to this example. 1st is the important part with using GetDiskFreeSpaceEx.

function DriveSpace(DriveLetter : String; var FreeSpace, UsedSpace, TotalSpace : int64) : Boolean;
begin
  Result := SysUtils.GetDiskFreeSpaceEx(Pchar(DriveLetter), UsedSpace, TotalSpace, @FreeSpace);

  if UsedSpace > 0 then
    UsedSpace := TotalSpace - FreeSpace;

  if not Result then
  begin
    UsedSpace   := 0;
    TotalSpace  := 0;
    FreeSpace   := 0;
  end;
end;

, , C: , .

:

var
  FS,
  US,
  TS : Int64
begin
  DriveSpace('C:', FS, US, TS);
  //Do something with the 3 variables.
end;

, , - :

procedure ListDrivesOfType(DriveType : Integer; var Drives : TStringList);
var
  DriveMap,
  dMask : DWORD;
  dRoot : String;
  I     : Integer;
begin
  dRoot     := 'A:\'; //' // work around highlighting
  DriveMap  := GetLogicalDrives;
  dMask     := 1;

  for I := 0 to 32 do
  begin
    if (dMask and DriveMap) <> 0 then
      if GetDriveType(PChar(dRoot)) = DriveType then
      begin
        Drives.Add(dRoot[1] + ':');
      end;

    dMask := dMask shl 1;
    Inc(dRoot[1]);
  end;
end;

DriveType, :

DRIVE_UNKNOWN     = 0;
DRIVE_NO_ROOT_DIR = 1;
DRIVE_REMOVABLE   = 2;
DRIVE_FIXED       = 3;
DRIVE_REMOTE      = 4;
DRIVE_CDROM       = 5;
DRIVE_RAMDISK     = 6;

( windows.pas)


, , ( ), ( memo1) FIXED HARD DRIVES:

Procedure TAform.SomeNameICantThinkOfNow;
const
  BytesPerMB = 1048576;
var
  MyDrives   : TStringlist;
  I : Integer;
  FreeSpace,
  UsedSpace,
  TotalSpace : int64;
begin
  MyDrives := TStringlist.Create;
  ListDrivesOfType(DRIVE_FIXED, MyDrives);

  Memo1.Lines.Clear;

  for I := 0 to MyDrives.Count - 1 do
  begin
    FreeSpace  := 0;
    UsedSpace  := 0;
    TotalSpace := 0;

    if DriveSpace(MyDrives.Strings[I], FreeSpace, UsedSpace, TotalSpace) then
    begin
      FreeSpace  := FreeSpace  div BytesPerMB;
      UsedSpace  := UsedSpace  div BytesPerMB;
      TotalSpace := TotalSpace div BytesPerMB;

      Memo1.Lines.Add('Drive: ' + MyDrives.Strings[I] + ' = Free Space :' + IntToStr(FreeSpace) +
                      ' Used Space: ' + IntToStr(UsedSpace) + ' Total Space: ' + IntToStr(TotalSpace));
    end;
  end;
end;

, ! IDE, , MB, Double , , MB , , , , , .

, .

+14

All Articles