I want my app to be notified, when the user inserts a USB drive, and also get the USB drives proper drive letter and path.
There is a windows api you can tap into that will let you know when a device is inserted.
Its been a long time since I had to do this so I don’t recall what it even is called any more.
But this would be a good start
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363217(v=vs.85).aspx
you can use WinUSBNotificationMBS class to get notified about USB devices being added or removed.
and for the drive letter, check this blog entry:
http://www.mbsplugins.de/archive/2012-02-24/Detecting_drive_letter_for_an_/monkeybreadsoftware_blog_archive
Theres code in the Windows Functionality Suite to make it so you can register to be notified of devices added & removed as well
These two examples might give you a hint
First time you plugin your memorystick it can take few seconds before anything is displayed when you run the code… If you want this to check every 1 seconds you need to add this code into a timer
Example 1: Search for USB devices.
// http://library.wmifun.net/cimv2/win32_diskdrive.html
//http://msdn.microsoft.com/en-us/library/aa394132%28v=vs.85%29.aspx
Dim locator, objWMIService, objs, objProperty As OLEOBJECT
Dim nobjs as Integer
// Connect to WMI
locator = new oleObject("WbemScripting.SWbemlocator", true)
Dim wmiServiceParams(2) as variant
wmiServiceParams(1) = "."
wmiServiceParams(2) = "root\\cimv2"
objWMIService= locator.invoke("ConnectServer", wmiServiceParams)
// Run the WMI query
objs = objWMIService.ExecQuery ("SELECT * FROM Win32_DiskDrive where InterfaceType='USB' ")
nobjs = objs.count - 1
msgbox ("Number of USB: " + str(nobjs +1))
For i as integer = 0 to nobjs
Dim stringData As String
objProperty = objs.ItemIndex(i)
// ItemIndex() is not supported in Windows XP only from Windows Vista and upwards
stringData = "Serial No: " + objProperty.Value("SerialNumber") + EndOfLine _
+ "Caption: " + objProperty.Value("Caption") + EndOfLine _
+ "DeviceID: " + objProperty.Value("DeviceID") + EndOfLine _
+ "Availability: " + objProperty.Value("Availability") + EndOfLine _
+ "Description: " + objProperty.Value("Description") + EndOfLine _
+ "MediaType: " + objProperty.Value("MediaType") + EndOfLine _
+ "Model: " + objProperty.Value("Model") + EndOfLine _
+ "Name: " + objProperty.Value("Name") + EndOfLine _
+ "Size: " + objProperty.Value("Size") + EndOfLine _
+ "Status: " + objProperty.Value("Status") + EndOfLine _
+ "StatusInfo: " + objProperty.Value("StatusInfo") + EndOfLine _
+ "SystemName: " + objProperty.Value("SystemName") + EndOfLine _
+ "Partitions: " + objProperty.Value("Partitions") + EndOfLine _
+ "CreationClassName: " + objProperty.Value("CreationClassName") + EndOfLine _
+ "InterfaceType: " + objProperty.Value("InterfaceType") + EndOfLine _
+ "PNPDeviceID: " + objProperty.Value("PNPDeviceID") + EndOfLine
msgbox stringData
Next
locator = Nil
exception err as oleexception
msgbox err.message
Example 2: Search for removable disk
//http://library.wmifun.net/cimv2/win32_logicaldisk.html
//http://msdn.microsoft.com/en-us/library/aa394173%28v=vs.85%29.aspx
Dim locator, objWMIService, objs, objProperty As OLEOBJECT
Dim nobjs as Integer
// Connect to WMI
locator = new oleObject("WbemScripting.SWbemlocator", true)
Dim wmiServiceParams(2) as variant
wmiServiceParams(1) = "."
wmiServiceParams(2) = "root\\cimv2"
objWMIService= locator.invoke("ConnectServer", wmiServiceParams)
// Run the WMI query
objs = objWMIService.ExecQuery ("SELECT * FROM Win32_LogicalDisk WHERE DriveType='2' ") // 2 = Removable Disk
nobjs = objs.count - 1
msgbox ("Number of Removable Disks: " + str(nobjs +1))
For i as integer = 0 to nobjs
Dim stringData As String
objProperty = objs.ItemIndex(i)
// ItemIndex() is not supported in Windows XP only from Windows Vista and upwards
stringData = "Caption: " + objProperty.Value("Caption") + EndOfLine _
+ "DeviceID: " + objProperty.Value("DeviceID") + EndOfLine _
+ "Availability: " + objProperty.Value("Availability") + EndOfLine _
+ "Description: " + objProperty.Value("Description") + EndOfLine _
+ "MediaType: " + objProperty.Value("MediaType") + EndOfLine _
+ "DriveType: " + objProperty.Value("DriveType") + EndOfLine _
+ "Name: " + objProperty.Value("Name") + EndOfLine _
+ "Size: " + objProperty.Value("Size") + EndOfLine _
+ "Status: " + objProperty.Value("Status") + EndOfLine _
+ "StatusInfo: " + objProperty.Value("StatusInfo") + EndOfLine _
+ "SystemName: " + objProperty.Value("SystemName") + EndOfLine _
+ "CreationClassName: " + objProperty.Value("CreationClassName") + EndOfLine _
+ "PNPDeviceID: " + objProperty.Value("PNPDeviceID") + EndOfLine _
+ "VolumeName: " + objProperty.Value("VolumeName") + EndOfLine _
+ "VolumeSerialNumber: " + objProperty.Value("VolumeSerialNumber") + EndOfLine _
+ "FileSystem: " + objProperty.Value("FileSystem") + EndOfLine
msgbox stringData
Next
locator = Nil
Thanks John, this code works perfectly for something I’d like to do!