检查 objDisk.MediaType。在这里你会找到一个 MediaTypes 列表;乍一看,MediaType 1 ... 10 表示“普通”软盘;在对我的(虚拟)机器进行快速检查时,USB 驱动器显示的 MediaType 为 Null(对于未知,甚至为零),因此您必须小心。再看一眼(谈论谨慎):大多数定义的媒体类型都标识软盘(其中一些是异国情调的)。顺便说一句 - USB 软盘驱动器呢?
由于我无法在“真实”计算机上进行测试,因此您必须仔细检查以下代码:
Const cnRemovableDisk = 2
Const cnMTypeUnknown = 0
Const cnMTypeNoFloppy = 11
Const cnMTypeFixedHD = 12
Dim strComputer : strComputer = "."
Dim objWMIService : Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Dim colDisks : Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")
Dim Removable : Removable = ""
Dim Floppy : Floppy = ""
Dim USBDrive : USBDrive = ""
Dim objDisk
For Each objDisk in colDisks
If objDisk.DriveType = cnRemovableDisk Then
Removable = Removable & ";" & objDisk.DeviceID & "\"
Select Case True
Case IsNull( objDisk.MediaType )
WScript.Echo objDisk.DeviceID, "has MediaType null - assuming USB Drive."
USBDrive = USBDrive & ";" & objDisk.DeviceID & "\"
Case objDisk.MediaType = cnMTypeNoFloppy
WScript.Echo objDisk.DeviceID, "has MediaType 11 - assuming USB Drive."
USBDrive = USBDrive & ";" & objDisk.DeviceID & "\"
Case objDisk.MediaType = cnMTypeUnknown
WScript.Echo objDisk.DeviceID, "has MediaType 0 - assuming USB Drive."
USBDrive = USBDrive & ";" & objDisk.DeviceID & "\"
Case objDisk.MediaType = cnMTypeFixedHD
WScript.Echo objDisk.DeviceID, "has MediaType 12 - how can this happen?"
Case Else
WScript.Echo objDisk.DeviceID, "has MediaType", objDisk.MediaType, " - surely some kind of floppy."
Floppy = Floppy & ";" & objDisk.DeviceID & "\"
End Select
End If
Next
Removable = Mid( Removable, 2 )
Floppy = Mid( Floppy , 2 )
USBDrive = Mid( USBDrive , 2 )
WScript.Echo "Removable:", Removable
WScript.Echo "Floppy: ", Floppy
WScript.Echo "USBDrive: ", USBDrive
我的输出是:
A: has MediaType 5 - surely some kind of floppy.
F: has MediaType null - assuming USB Drive.
Removable: A:\;F:\
Floppy: A:\
USBDrive: F:\
我的 USBDrive 的 null MediaType 可能是一个奇怪的意外。我试图通过使用“Select Case True ”控制结构来轻松修改 MediaType 的评估。VBScript 将测试 Cases 的条件,直到第一个为真,执行相应的语句,并“中断”到 End Select。因此,添加特殊情况和/或重新排序情况很简单——只需将 IsNull 检查保持在第一位。