gpt4 book ai didi

delphi - 如何使用 WMI 和 Delphi 关联逻辑驱动器和物理磁盘?

转载 作者:行者123 更新时间:2023-12-03 15:39:21 28 4
gpt4 key购买 nike

我已经在一个可以从 Micro 读取信息的应用程序上进行了很长时间的工作,一些(大多数)失败了,更多仍然缺少一些,我正在使用 WMI 和 Delphi。我面临的问题是我需要列出属于每个 HD 的单元,例如:HD1 具有驱动器 C:、D: 等。

谢谢

最佳答案

要关联逻辑驱动器和物理磁盘,您必须使用 Win32_DiskDrive类和 Win32_DiskDriveToDiskPartition , Win32_LogicalDiskToPartition ASSOCIATORS类。

尝试这个示例。

{$APPTYPE CONSOLE}

uses
SysUtils,
ActiveX,
ComObj,
Variants;


function ListDrives : string;
var
FSWbemLocator : OLEVariant;
objWMIService : OLEVariant;
colDiskDrives : OLEVariant;
colLogicalDisks: OLEVariant;
colPartitions : OLEVariant;
objdiskDrive : OLEVariant;
objPartition : OLEVariant;
objLogicalDisk : OLEVariant;
oEnumDiskDrive : IEnumvariant;
oEnumPartition : IEnumvariant;
oEnumLogical : IEnumvariant;
iValue : LongWord;
DeviceID : string;
begin;
Result:='';
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
objWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
colDiskDrives := objWMIService.ExecQuery('SELECT DeviceID FROM Win32_DiskDrive');
oEnumDiskDrive := IUnknown(colDiskDrives._NewEnum) as IEnumVariant;
while oEnumDiskDrive.Next(1, objdiskDrive, iValue) = 0 do
begin
Writeln(Format('DeviceID %s',[string(objdiskDrive.DeviceID)]));
//Escape the `\` chars in the DeviceID value because the '\' is a reserved character in WMI.
DeviceID := StringReplace(objdiskDrive.DeviceID,'\','\\',[rfReplaceAll]);
//link the Win32_DiskDrive class with the Win32_DiskDriveToDiskPartition class
colPartitions := objWMIService.ExecQuery(Format('ASSOCIATORS OF {Win32_DiskDrive.DeviceID="%s"} WHERE AssocClass = Win32_DiskDriveToDiskPartition',[DeviceID]));
oEnumPartition := IUnknown(colPartitions._NewEnum) as IEnumVariant;
while oEnumPartition.Next(1, objPartition, iValue) = 0 do
begin
if not VarIsNull(objPartition.DeviceID) then
begin
Writeln(Format(' Partition %s',[string(objPartition.DeviceID)]));
//link the Win32_DiskPartition class with theWin32_LogicalDiskToPartition class.
colLogicalDisks := objWMIService.ExecQuery('ASSOCIATORS OF {Win32_DiskPartition.DeviceID="'+VarToStr(objPartition.DeviceID)+'"} WHERE AssocClass = Win32_LogicalDiskToPartition');
oEnumLogical := IUnknown(colLogicalDisks._NewEnum) as IEnumVariant;
while oEnumLogical.Next(1, objLogicalDisk, iValue) = 0 do
begin
Writeln(Format(' Logical Disk %s',[string(objLogicalDisk.DeviceID)]));
objLogicalDisk:=Unassigned;
end;
end;
objPartition:=Unassigned;
end;
objdiskDrive:=Unassigned;
end;
end;

begin
try
CoInitialize(nil);
try
ListDrives;
finally
CoUninitialize;
end;
except
on E:Exception do
Writeln(E.Classname, ':', E.Message);
end;
Readln;
end.

这将输出类似这样的内容

DeviceID \\.\PHYSICALDRIVE0
Partition Disk #0, Partition #0
Logical Disk F:
DeviceID \\.\PHYSICALDRIVE1
Partition Disk #1, Partition #0
Logical Disk C:

关于delphi - 如何使用 WMI 和 Delphi 关联逻辑驱动器和物理磁盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12271269/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com