zoukankan      html  css  js  c++  java
  • 可以用WMI来获取磁盘及分区编号

    {$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', 'rootCIMV2', '', '');
      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)]));
          DeviceID        := StringReplace(objdiskDrive.DeviceID,'','\',[rfReplaceAll]);
          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)]));
            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.
  • 相关阅读:
    phpcms调用指定文章内容模型的ID
    phpcms V9的各种功能总结
    如何使用ajax与php实现简单的流程管理
    键盘按键和键盘对应代码表
    ajax实现省、市、区、三级联动(例题)
    用ajax做登录与删除
    var_dump、echo、print_r 的区别
    字符串删除重复字符
    树的非递归遍历
    字符串操作_(不使用库函数)
  • 原文地址:https://www.cnblogs.com/CodeGear/p/5294318.html
Copyright © 2011-2022 走看看