Find which drive corresponds to which USB mass storage device in WinXP

后端 未结 2 1259
野的像风
野的像风 2020-12-08 17:53

I have several USB drives connected to a WinXP SP3 computer, and I need to tell them apart programatically - I need to find which drive letter corresponds to which d

相关标签:
2条回答
  • 2020-12-08 17:56

    There is an official API to do this. See this sample: http://www.codeproject.com/KB/system/RemoveDriveByLetter.aspx

    The principle is that each storage volume on Windows is assigned a global number. First, you ask your drive for its number (by opening "\X:" and sending a IOCTL_STORAGE_GET_DEVICE_NUMBER request). Second, you go over all disk devices and ask each one for its number (by opening it, through a different path, so you can't just string-compare).

    Once you find a disk device carrying the same number as you queried your drive, you know you have a winner.

    0 讨论(0)
  • 2020-12-08 18:00

    It's a non-trivial question. There is no official API for it, as far as I can tell. So, you need an undocumented API: the registry. HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices contains entries for both drive letters and volume IDs. If you look at the actual data, you'll find that it identifies the drive. Look at the binary data as a Unicode string. It will point you to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ENUM\. However, XP and Vista differ in exactly what subkeys are referenced there, and how. (Vista is easier, so try that first).

    0 讨论(0)
提交回复
热议问题