我面临的问题是我需要列出属于每个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:总结
以上是内存溢出为你收集整理的如何使用WMI和Delphi关联逻辑驱动器和物理磁盘?全部内容,希望文章能够帮你解决如何使用WMI和Delphi关联逻辑驱动器和物理磁盘?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)