开发者

Delphi - How to get total disk space of Windows drive?

开发者 https://www.devze.com 2023-03-14 01:41 出处:网络
I need to get total disk space in Delphi progra开发者_StackOverflowm.Use DiskSize and DiskFree functions for this problem.

I need to get total disk space in Delphi progra开发者_StackOverflowm.


Use DiskSize and DiskFree functions for this problem. ComboBox1 contains a list of drives letters.

var
  Disk: Integer;
...
procedure TForm1.Button1Click(Sender: TObject);
var
  Total, Free: LongInt;
begin
  Total:=DiskSize(Disk) div 1024;
  Free:=DiskFree(Disk) div 1024;
  Gauge1.MaxValue:=Total;
  Gauge1.Progress:=Free;
  Label1.Caption:='Total size - '+IntToStr(Total);
  Label2.Caption:='Free - '+IntToStr(Free);
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  Disk:=ComboBox1.ItemIndex+1;
end;


Here you have another option using the Win32_LogicalDisk WMI class

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ActiveX,
  ComObj,
  Variants;

procedure  GetWin32_LogicalDiskSize(const drive: string);
const
  WbemUser            ='';
  WbemPassword        ='';
  WbemComputer        ='localhost';
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
  FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM Win32_LogicalDisk Where Caption=%s',[QuotedStr(drive)]),'WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  if oEnum.Next(1, FWbemObject, iValue) = 0 then
  begin
    Writeln(Format('FreeSpace  %s Bytes',[FormatFloat('#,',FWbemObject.FreeSpace)]));// Uint64
    Writeln(Format('Size       %s Bytes',[FormatFloat('#,',FWbemObject.Size)]));// Uint64
    FWbemObject:=Unassigned;
  end;
end;


begin
 try
    CoInitialize(nil);
    try
      GetWin32_LogicalDiskSize('C:');
    finally
      CoUninitialize;
    end;
 except
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.


Some may find this useful - it will give you total and free disk space by Volume's DeviceID. It also uses WMI like RRUZ's answer, but it works with DeviceID, so it has the same advantages + it also works with unmaped devices and drivers (or mapped to path).

procedure DiskSizesFromVolumeDeviceID(deviceID:string);
var
  WMIServices : ISWbemServices;
  Root        : ISWbemObjectSet;
  Item: Variant;
  i: Integer;
  Cap:TCap;
begin
  try
  WMIServices := CoSWbemLocator.Create.ConnectServer('.', 'root\cimv2','', '', '', '', 0,     nil);
  Root  := WMIServices.ExecQuery(Format('SELECT Capacity, FreeSpace FROM Win32_Volume     WHERE DeviceID="%s"', [StringReplace(deviceID, '\', '\\', [rfReplaceAll])]),'WQL', 0, nil);
  for i := 0 to Root.Count - 1 do
  begin
    Item := Root.ItemIndex(i);

    TotalSpace := ( Item.Capacity);
    FreeSpace := (Item.FreeSpace);
  End;
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消