如何在Autohotkey中找到USB连接的可移动硬盘
我正在尝试查找连接到我的系统的所有USB硬盘驱动器/SSD 命令 DriveGet,键入 应该返回这些值 未知、可移动、固定、网络、CDROM、RAM磁盘。 下面的脚本为每个驱动器返回“已修复”,无论其连接方式如何 有办法解决这个问题吗如何在Autohotkey中找到USB连接的可移动硬盘,autohotkey,Autohotkey,我正在尝试查找连接到我的系统的所有USB硬盘驱动器/SSD 命令 DriveGet,键入 应该返回这些值 未知、可移动、固定、网络、CDROM、RAM磁盘。 下面的脚本为每个驱动器返回“已修复”,无论其连接方式如何 有办法解决这个问题吗 DriveGet, DriveList , List, Loop, { MyDrive := SubStr(Drivelist, A_Index,1) If (MyDrive = "") break MyDrive = %MyD
DriveGet, DriveList , List,
Loop,
{
MyDrive := SubStr(Drivelist, A_Index,1)
If (MyDrive = "")
break
MyDrive = %MyDrive%
DriveGet, MyLabel, serial, %MyDrive%
DriveGet, MyType, Type, %MyDrive%:\
msgbox, Drive %MyDrive% Type %MyType%
}
操作系统读取可移动驱动器上的一个位,并决定它们是否可移动(第一个字节的第7位)
最有可能的情况是驱动器未设置为可移动,因此除非您重写位,否则它们不是解决方案。重写位通常是不可能的,因为它在控制器上,而不是闪存存储空间上 这似乎是一个在自动热键论坛上使用中的脚本解决的问题。我已经包含了下面线程中的脚本。试试看
#NoEnv
#SingleInstance force
SetBatchLines -1
ListLines Off
SendMode Input
SetWorkingDir %A_ScriptDir%
pd := PhysicalFromLogical("F") ; This is the drive you want to test
if GetType(pd) = "Fixed" and GetInterface(pd) = "USB"
MsgBox Drive is Fixed and USB
else
MsgBox Drive is either not Fixed or not USB
return
; Given a drive letter like "f" return the physical
; drive associated with it, i.e. \\\\.\\PHYSICALDRIVE2
PhysicalFromLogical(d)
{
wmi := ComObjGet("winmgmts:")
for LogicalDisk in wmi.ExecQuery("Select * from Win32_LogicalDiskToPartition")
if InStr(LogicalDisk.Dependent,d)
for Partition in wmi.ExecQuery("Select * from Win32_DiskDriveToDiskPartition")
if (Partition.Dependent = LogicalDisk.Antecedent) {
Start := InStr(Partition.Antecedent, """") + 1
return SubStr(Partition.Antecedent, Start, -1)
}
return 0
}
; Given a drive path like \\\\.\\PHYSICALDRIVE2 return the
; drives interface type, i.e. "USB"
GetInterface(pd)
{
wmi := ComObjGet("winmgmts:")
for Drive in wmi.ExecQuery("Select * from Win32_DiskDrive where DeviceId = """ pd """")
return Drive.InterfaceType
return 0
}
; Given a drive path like \\\\.\\PHYSICALDRIVE2 return the drive type, i.e. "Removable"
; This is just a wrapper for DriveGet
GetType(pd)
{
StringReplace pd, pd, \\, \, All
DriveGet out, Type, %pd%
return out
}
谢谢,这就解释了为什么AHK报告驱动器是固定的,即使驱动器不是。重写位将不起作用,因为脚本的全部目的是在状态未知时确定驱动器是否可移动。因此,也许其他人对如何确定某个驱动器是否通过USB连接有想法;除非硬盘是分区的。目前在我的系统上,驱动器C、E、F是固定的(两个分区硬盘),但如果我卸下第二个驱动器,这可能会改变。因此,我真的在寻找一种方法,在不手动将此信息提供给系统的情况下,确定驱动器是否连接了USB。(这将是欺骗…)