本帖最后由 sunkiss 于 2013-11-5 14:46 编辑
用注册表“KEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\usbstor\Enum”,到是能读取了,可是如果有多个U盘,就不知道哪一个是我所在盘符的U盘了。WMI中“$objWMIService.ExecQuery("Select * From Win32_USBHub")”,在XP下获得失败啊。还有什么办法呢?自己解决了。QueryDiskNum("k:")
Func QueryDiskNum($d);代入盘符,如:K:
Dim $JB = DriveGetLabel($d), $LX = DriveGetFileSystem($d)
$objWMIService = ObjGet("winmgmts:\\.\root\cimv2")
$CPXHS = $objWMIService.ExecQuery("Associators of {win32_LogicalDisk.DeviceID='" & $d & "'} where ResultClass = Win32_DiskPartition")
For $CPXH In $CPXHS
Dim $XH = $CPXH.DiskIndex
$CXVIDPIDS = $objWMIService.ExecQuery("Select * from Win32_DiskDrive Where Index=" & $XH)
For $CXVPX In $CXVIDPIDS
$JKLX = $CXVPX.InterfaceType
$PNPDID = StringSplit($CXVPX.PNPDeviceID, "");获得PNPDeviceID
If $CXVPX.InterfaceType = "USB" Then
$USBKEY = StringLeft($PNPDID[$PNPDID[0]], StringLen($PNPDID[$PNPDID[0]]) - 2)
$VPH = $objWMIService.ExecQuery("Select * From Win32_USBHub")
For $VP In $VPH
;MsgBox(0, "sdf", $VP.DeviceID)
;MsgBox(0, "sdf", $VP.PNPDeviceID)
If StringInStr($VP.PNPDeviceID, $USBKEY) Then;从PNPDeviceID中,判断有没有刚才的序列号,有就说明存在这个U盘。并能读取PID和VID
$PNPDID = StringSplit($VP.PNPDeviceID, "")
$PNPDID = StringSplit($PNPDID[$PNPDID[0] - 1], "&")
Dim $VID = StringRight($PNPDID[$PNPDID[0] - 1], 4), $PID = StringRight($PNPDID[$PNPDID[0]], 4), $UPCS = StringReplace($CXVPX.Model, "USB DEVICE", "")
EndIf
Next
EndIf
Next
Next
If DriveGetType($d) = "Removable" Then
$SFYD = "可移动磁盘"
ElseIf DriveGetType($d) = "Fixed" Then
$SFYD = "固定磁盘"
ElseIf DriveGetType($d) = "CDROM" Then
$SFYD = "光驱"
Else
$SFYD = "未知类型"
EndIf
$DX = Round(DriveSpaceTotal($d) / 1024, 2) & "GB"
EndFunc ;==>QueryDiskNum
|