我试图弄清楚如何检测驱动器是否是大容量存储设备。我可以得到驱动器号,但无法弄清楚如何检测它是什么类型的设备。我试图检测Garmin GPS接收器是否以大容量存储模式连接到PC。
答案 0 :(得分:4)
您可以使用WMI执行此任务,存在几个可以帮助您确定信息的类,从Win32_USBHub
检查属性中的值Mass Storage Device
开始em>描述,也看看Win32_PNPEntity
类。如果您没有从Delphi访问WMI的经验,请尝试使用Wmi delphi code creator
<强>更新强>
要将Win32_USBHub或Win32_USBControllerDevice WMI类返回的值与磁盘驱动器号相关联,您必须执行以下步骤
DeviceID
属性的dependent
检查Win32_PnPEntity类,使用WQL句子链接到Win32_DiskDrive,如下所示:ASSOCIATORS OF {Win32_PnPEntity.DeviceID="DeviceID"} WHERE ResultClass = Win32_DiskDrive
检查此示例代码
{$APPTYPE CONSOLE}
{$R *.res}
uses
Types,
StrUtils,
SysUtils,
ActiveX,
ComObj,
Variants;
procedure ScanUSBPnpDevices;
const
wbemFlagForwardOnly = $00000020;
var
FSWbemLocator : OLEVariant;
objWMIService : OLEVariant;
USBControllerDevices: OLEVariant;
USBControllerDevice : OLEVariant;
EnumUSBDevice : IEnumvariant;
PnPEntities : OLEVariant;
PnPEntity : OLEVariant;
EnumPnPEntity : IEnumvariant;
DiskDrives : OLEVariant;
DiskDrive : OLEVariant;
EnumDiskDrive : IEnumvariant;
DiskPartitions : OLEVariant;
DiskPartition : OLEVariant;
EnumDiskPartition : IEnumvariant;
iValue : LongWord;
DeviceID : string;
DiskDeviceID : string;
DiskPartID : string;
StringDynArray : TStringDynArray;
begin;
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
objWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
//This will scan all the usb device, you can filter this WQL sentece using any property of this class, to speed-up the process.
USBControllerDevices := objWMIService.ExecQuery('SELECT Dependent FROM Win32_USBControllerDevice','WQL',wbemFlagForwardOnly);
EnumUSBDEvice := IUnknown(USBControllerDevices._NewEnum) as IEnumVariant;
while EnumUSBDEvice.Next(1, USBControllerDevice, iValue) = 0 do
begin
StringDynArray:=SplitString(USBControllerDevice.Dependent, '=');
DeviceID:=StringDynArray[1];
Writeln(Format('USB Controller Device Device ID %s',[DeviceID]));
PnPEntities := objWMIService.ExecQuery(Format('ASSOCIATORS OF {Win32_PnPEntity.DeviceID=%s} WHERE ResultClass = Win32_DiskDrive',[DeviceID]),'WQL',wbemFlagForwardOnly);
EnumPnPEntity := IUnknown(PnPEntities._NewEnum) as IEnumVariant;
while EnumPnPEntity.Next(1, PnPEntity, iValue) = 0 do
begin
//Escape the `\` chars in the DeviceID value because the '\' is a reserved character in WMI.
DiskDeviceID := PnPEntity.DeviceId;
Writeln(Format(' Disk Drive Device ID %s',[DiskDeviceID]));
DiskDeviceID := StringReplace(DiskDeviceID,'\','\\',[rfReplaceAll]);;
DiskDrives := objWMIService.ExecQuery(Format('ASSOCIATORS OF {Win32_DiskDrive.DeviceID="%s"} WHERE AssocClass = Win32_DiskDriveToDiskPartition',[DiskDeviceID]),'WQL',wbemFlagForwardOnly);
EnumDiskDrive := IUnknown(DiskDrives._NewEnum) as IEnumVariant;
while EnumDiskDrive.Next(1, DiskDrive, iValue) = 0 do
begin
DiskPartID:=DiskDrive.deviceID;
Writeln(Format(' Disk Partition ID %s',[DiskPartID]));
DiskPartitions:=objWMIService.ExecQuery(Format('ASSOCIATORS OF {Win32_DiskPartition.DeviceID="%s"} WHERE AssocClass = Win32_LogicalDiskToPartition',[DiskPartID]),'WQL',wbemFlagForwardOnly);
EnumDiskPartition := IUnknown(DiskPartitions._NewEnum) as IEnumVariant;
while EnumDiskPartition.Next(1, DiskPartition, iValue) = 0 do
begin
Writeln(Format(' Drive Letter %s',[String(DiskPartition.DeviceID)]));
DiskPartition:=Unassigned;
end;
DiskDrive:=Unassigned;
end;
PnPEntity:=Unassigned;
end;
USBControllerDevice:=Unassigned;
end;
end;
begin
try
CoInitialize(nil);
try
ScanUSBPnpDevices;
finally
CoUninitialize;
end;
except
on E:Exception do
Writeln(E.Classname, ':', E.Message);
end;
Readln;
end.
哪些会返回这样的东西。
USB Controller Device Device ID "USBSTOR\\DISK&VEN_HP&PROD_V100W&REV_1.00\\3S980
62800DD&0"
Disk Drive Device ID \\.\PHYSICALDRIVE1
Disk Partition ID Disk #1, Partition #0
Drive Letter F: