# Make it work for Python2 and Python3
if sys.version_info[0]<3:
from _winreg import *
else:
from winreg import *
# Get DOS devices (connected or not)
def get_dos_devices():
ddevs=[dev for dev in get_mounted_devices() if 'DosDevices' in dev[0]]
return [(d[0], regbin2str(d[1])) for d in ddevs]
# Get all mounted devices (connected or not)
def get_mounted_devices():
devs=[]
mounts=OpenKey(HKEY_LOCAL_MACHINE, 'SYSTEM\MountedDevices')
for i in range(QueryInfoKey(mounts)[1]):
devs+=[EnumValue(mounts, i)]
return devs
# Decode registry binary to readable string
def regbin2str(bin):
str=''
for i in range(0, len(bin), 2):
if bin[i]<128:
str+=chr(bin[i])
return str
最后,我使用了一种更简单的方法。在
我使用Python附带的
winreg
模块来访问Windows注册表。HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices
跟踪安装的所有设备(当前是否连接)。所以我从那里得到所有的设备信息,并检查设备当前是否连接,我只需os.path.exists
设备的存储字母(即G:
)。存储字母可以从密钥MountedDevices
获得。在示例:
然后简单地运行:
^{pr2}$相关问题 更多 >
编程相关推荐