Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi - Enumerate the Disk(s) and other Drives on Windows PC [duplicate]

Tags:

winapi

delphi

Possible Duplicate:
Get drive information (free space, etc.) for drives on Windows and populate a memo box

I am quite new to programing (especially Delphi), and have been unable to find any examples as to how to enumerate all the drives on a PC.

I really care about Hard Disks and CD-Rom drives but I have been unable to find anything useable.

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

like image 820
DelphiDelver Avatar asked Apr 12 '11 13:04

DelphiDelver


2 Answers

The simplest way is actually to use GetDiskFreeSpaceEx from the sysutils.pas file.

There are 2 parts to this example. the 1st is the important part 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;

If you are going to request drives that you already know the drive letter for such as C: then that is all you need.

Usage would be something like:

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

Having said that if you want to find the drives as well you could use something like this:

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;

Note the DriveType integer, should be one of the following:

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

(I have taken these straight out of windows.pas)


Now finally to answer your question (and this is very rough) the following would add information into a memo (called memo1) for all 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;

I did say it would be nasty! I have just run this up in the IDE and it works, I have done as MB but really you should convert to Double and choose your formatting if doing as MB to be more precise as the example I have create above will of course just round up.

Hope this is of some small assistance.

like image 53
Reallyethical Avatar answered Sep 21 '22 17:09

Reallyethical


See GLibWMI Library; With It you can access information about system drives.

like image 27
Germán Estévez -Neftalí- Avatar answered Sep 19 '22 17:09

Germán Estévez -Neftalí-