Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Detect if usb device is inserted

Tags:

c#

c#-4.0

usb

I'd like to now if it is possible to only allow a C# application run if a usb device is inserted? The usb device is a Pololu micro servo controler. At the moment I'm cheking if it is connected throug the serial conection if the Pololu is sending a "Char" as I programed It. But some times the Pololu is connected but in a part of the loop where the Char is not sent, so in this case the application won't open even with the pololu connected. Is there a more reliable way to detect it?

like image 297
Aman Avatar asked Nov 13 '22 01:11

Aman


1 Answers

You can get the removable drives through the following code.

 using System.IO.DriveInfo;

 var availableDrives = DriveInfo.GetDrives()
.Where(d=> d.IsReady && d.DriveType == DriveType.Removable);

details of DriveInfo You can also take a quick look at open source project LibUsbDotNet

like image 105
Ehsan Avatar answered Nov 14 '22 23:11

Ehsan