4

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

谢谢你

4

2 回答 2

12

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

试试这个样本。

{$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:
于 2012-09-04T21:45:11.443 回答
2

使用 MagWMi 包装器及其单线功能(单线很慢 - 但这不是需要速度的地方)。我用它来制作 2 级磁盘列表。

  • 硬盘 1 -> 字母 1、字母 2
  • 硬盘 2 -> 字母 3、字母 4
  • NetServer 1 -> 字母 5、字母 6
  • NetServer 2 -> 字母 7、字母 8
  • 无法识别 -> 所有其他字母

但是您必须记住,有些磁盘可能没有任何字母就可以使用。所以这会告诉你有关根目录的信息,但不会更多。磁盘 C:可以生成多个物理磁盘。并且某些物理磁盘可能根本不用字母。

unit WMI_Helper;

interface

function WMINetDiskName(const disk: string { 'C:' - w/o slash } ): string;
function WMIPhysDiskName(const disk: string { 'C:' - w/o slash } ): string;
function WMIGetVolumeName(const disk: string { 'C:' - w/o slash } ): string;

implementation

uses magwmi, SysUtils, StrUtils, Windows, IOUtils;

function WMIGetProp(const query, param, resultProp: string): string;
begin
  if MagWmiGetOneQ(StringReplace(query, '%s', param, []), resultProp, Result) <= 0
  then
    Result := '';
  Result := Trim(Result);
end;

function WMINetDiskName(const disk: string { 'C:' - w/o slash } ): string;
const
  req = 'select ProviderName from Win32_MappedLogicalDisk where DeviceID = "%s"';
  prop = 'ProviderName';
var
  i: integer;
begin
  Result := WMIGetProp(req, disk, prop);

  If not TPath.IsUNCPath(Result) then
    exit('');

  i := PosEx('\', TPath.GetPathRoot(Result), 3);
  if i <= 0 then
    exit('');

  SetLength(Result, i - 1);
end;

function WMIPhysDiskName(const disk: string { 'C:' - w/o slash } ): string;
const
  resultProp = 'DeviceID';
  reqPart = 'ASSOCIATORS OF {Win32_LogicalDisk.DeviceID="%s"} WHERE ResultClass=Win32_DiskPartition';
  reqDisk = 'ASSOCIATORS OF {Win32_DiskPartition.DeviceID="%s"} WHERE ResultClass=Win32_DiskDrive';
begin
  Result := WMIGetProp(reqPart, disk, resultProp);
  if Result > '' then
    Result := WMIGetProp(reqDisk, Result, resultProp);
end;

function WMIGetVolumeName(const disk: string { 'C:' - w/o slash } ): string;
const
  prop = 'VolumeName';
  reqNet = 'select VolumeName from Win32_MappedLogicalDisk where DeviceID = "%s"';
  reqPhy = 'select VolumeName from Win32_LogicalDisk where DeviceID = "%s"';

begin
  Result := WMIGetProp(IfThen(GetDriveType(PChar(disk)) = DRIVE_REMOTE, reqNet,
    reqPhy), disk, prop);
end;

end.
于 2012-09-05T09:35:36.847 回答