How to Detect When an USB Device has been unplugged
Hi,
I am developing an application where I have to determine if an USB Connector assigned to a COM Port has been pulled out.
The obvious approach which I tried was to detect the available COM Ports using SerialPort.GetPortNames(). Unfortunately, even after physically pulling it out, the Port Name list still shows the same port being present.
Obviously, any operations would now fail as the connection is no longer available, but how to do this without making any communication, that is, without sending or receiving data.
Re: How to Detect When an USB Device has been unplugged
http://www.pinvoke.net/default.aspx/...ification.html
should contain everything you need, or at least get you started.
Re: How to Detect When an USB Device has been unplugged
Thanks for the reply.
That would be one way to do this task.
However, one thing that I don't understand is why SerialPort.GetPortNames() will still show a COM that has been removed.
Re: How to Detect When an USB Device has been unplugged
The good old API route :
RegisterDeviceNotification
UnRegisterDeviceNotification
As pointed out by mariocatch ( :thumb: ) is the best way to go