我需要在 Delphi 程序中获取总磁盘空间。
8yparm6h1#
请使用“大小”和“自由”函数解决此问题。ComboBox1包含驱动器号列表。
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;
bjp0bcyl2#
在这里,您可以使用Win32_LogicalDisk xml2类进行另一种选择
Win32_LogicalDisk
{$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.
i5desfxk3#
有些人可能会发现这很有用-它将给予您的总和自由磁盘空间的卷的设备ID。它也使用类似于RRUZ的应答,但它与DeviceID一起工作,因此它具有相同的优点+它也与未Map的设备和驱动程序(或Map到路径)一起工作。
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;
qaxu7uf24#
使用此代码的最佳方法:
var lpFreeBytesAvailableToCaller, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes : TLargeInteger; // Int64; begin ... GetDiskFreeSpaceExA(PChar(path_to_dir), lpFreeBytesAvailableToCaller, lpTotalNumberOfBytes, @lpTotalNumberOfFreeBytes); ... end;
4条答案
按热度按时间8yparm6h1#
请使用“大小”和“自由”函数解决此问题。ComboBox1包含驱动器号列表。
bjp0bcyl2#
在这里,您可以使用
Win32_LogicalDisk
xml2类进行另一种选择i5desfxk3#
有些人可能会发现这很有用-它将给予您的总和自由磁盘空间的卷的设备ID。它也使用类似于RRUZ的应答,但它与DeviceID一起工作,因此它具有相同的优点+它也与未Map的设备和驱动程序(或Map到路径)一起工作。
qaxu7uf24#
使用此代码的最佳方法: