Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the free USB ports in c#?

Tags:

c#

winapi

usb

wmi

I am developing C# application which needs to find any USB port is free for use or not.

Is there any C# class or WMI query to determine USB Port is free for use or not?

Suppose, If a machine has 5 USB ports and out of which 2 are already occupied by mouse ,keyboard and remaining 3 are free. So I need to identify these ports which are not in use.

Is there any win32 class for this so I can use P/Invoke

Regards,

Amol.

like image 796
user3138481 Avatar asked Oct 31 '22 23:10

user3138481


1 Answers

Take a look at that answer. I think you need to inspect Availability property of each Win32_USBHub to determine if device is connected to that port. However, there may be difficulties with USB hubs.

if ((int)device.GetPropertyValue("Availability")>3)
    //port is free
like image 130
cyberj0g Avatar answered Nov 08 '22 10:11

cyberj0g