檢查objDisk.MediaType。 Here你會找到一個MediaTypes列表;乍一看MediaType 1 ... 10表示'正常'軟盤;在我的(虛擬)機器上進行快速檢查時,USB驅動器顯示MediaType爲Null(甚至不知道爲零),因此您必須小心。乍看之下(談論carefull):大多數確定的媒體類型識別軟盤(其中一些是異國情調)。順便說一句 - 關於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的MediaType可能是一個奇怪的事故。我試圖通過使用「Select Case True」控制結構來評估MediaType的方法。 VBScript將測試案例的條件,直到第一個真實案例的條件,執行相應的語句,然後'終止'到結束選擇。因此,添加特殊情況和/或重新排序情況非常簡單 - 只需將IsNull檢查保留在第一個位置即可。
嗨Ekkehard.Horner,幹得好的人:) Huuuuuuuge謝謝。它工作正常。你很棒:)你能給我你的郵件嗎? – Nofuzy