Get List of Connected Usb Devices

Get List of connected USB Devices

Add a reference to System.Management for your project, then try something like this:

namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Management; // need to add System.Management to your project references.

class Program
{
static void Main(string[] args)
{
var usbDevices = GetUSBDevices();

foreach (var usbDevice in usbDevices)
{
Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}",
usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
}

Console.Read();
}

static List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
collection = searcher.Get();

foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
));
}

collection.Dispose();
return devices;
}
}

class USBDeviceInfo
{
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
}
public string DeviceID { get; private set; }
public string PnpDeviceID { get; private set; }
public string Description { get; private set; }
}
}

Get list of available USB devices in WebUSB API

There is no API to get a list of all available devices. A site can only get access to a device (and information about that device) by asking the user for permission.

I think there's some confusion about what navigator.usb.getDevices() does. It returns the set of connected devices the user has previously granted the site access to, whether they are claimed or not.

What I recommend applications do is use navigator.usb.getDevices() to show the user the set of devices that are available and have an "add device" button which will call navigator.usb.requestDevice() if the user doesn't see the device they are looking for. If the user knows they have two devices and need to connect to both of them they'd click the button twice.

Getting all available information about connected USB devices in windows and NET

The Properties collection property will contain all of the properties you can access.

https://msdn.microsoft.com/en-us/library/system.management.managementbaseobject.properties(v=vs.110).aspx

A CMD command to list the connected devices via USB?

You can use devcon to list the USBs:
%WindowsSdkDir%\tools\x64\devcon.exe /find *USB* - but you need Windows SDK installed for it to be available.

On Windows 10 you don't need the SDK and can use the pnputil /enum-devices | findstr USB to list the devices but this functionality isn't available on Win8.

You can also try wmic per @AlexK comment

How to get list of usb devices connected from a service

getApplicationContext does work in protected void onHandleIntent(Intent intent) so you have to use below method to get getApplicationContext-

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mContext = getApplicationContext();
return super.onStartCommand(intent, flags, startId);
}


Related Topics



Leave a reply



Submit