I need to get total disk space in Delphi program.
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;
Best way use this code:
var
lpFreeBytesAvailableToCaller,
lpTotalNumberOfBytes,
lpTotalNumberOfFreeBytes : TLargeInteger; // Int64;
begin
...
GetDiskFreeSpaceExA(PChar(path_to_dir), lpFreeBytesAvailableToCaller, lpTotalNumberOfBytes, @lpTotalNumberOfFreeBytes);
...
end;
Just thought I'd chime in in share the solution I ended up implementing based on what I've gathered in the accepted answer:
function GetDriveCapacity(const Drive: Integer): Int64;
begin
Result:= DiskSize(Drive);
end;
function GetDriveSpaceFree(const Drive: Integer): Int64;
begin
Result:= DiskFree(Drive);
end;
function GetDriveSpaceUsed(const Drive: Integer): Int64;
begin
Result:= GetDriveCapacity(Drive) - GetDriveSpaceFree(Drive);
end;
function GetDriveSpaceUsedPct(const Drive: Integer): Single;
var
U, C: Int64;
begin
try
U:= GetDriveSpaceUsed(Drive);
C:= GetDriveCapacity(Drive);
Result:= (U / C) * 100;
except
Result:= -1;
end;
end;
The core of everything here starts with GetDriveSpaceUsedPct
. This returns a float between 0 and 100 representing the percentage of space available on a disk.
NOTE: Refer to the documentation for DiskSize
and DiskFree
when it comes to using the Drive
parameter.
Usage:
procedure TForm1.TmrTimer(Sender: TObject);
var
Perc: Double;
begin
//C Drive Usage
Perc:= GetDriveSpaceUsedPct(3);
...
//D Drive Usage
Perc:= GetDriveSpaceUsedPct(4);
...
end;
© 2022 - 2024 — McMap. All rights reserved.