C# – find Device in C#


I need my code to respond to the event when my system finds new hardware like USB. How do I go about doing this in C#?

Best Solution

I don't think there are any .Net classes that can help you do this. So you must fall back on calling the Windows API directly.

When a device changes, Windows sends the WM_DEVICECHANGE message. Write your code to respond to this message.

See this MSDN page about WM_DEVICECHANGE. Also, for details on how to write code to respond to it, see this pdf.

To know about calling Windows API from a C# program, see this article.