Show Info: Console Application Description
Opens a USB device by vendor and product id.
Gets and displays the UsbDeviceDescriptor and the UsbConfigDescriptor.
Gets and displays the ManufacturerString, if one exists.
Gets and displays the ProductString, if one exists.
Gets and displays the SerialString, if one exists.
Example
CopyC#
using System; using LibUsbDotNet; using LibUsbDotNet.Info; using LibUsbDotNet.Main; using System.Collections.ObjectModel; namespace Examples { internal class ShowInfo { public static UsbDevice MyUsbDevice; public static void Main(string[] args) { // Dump all devices and descriptor information to console output. UsbRegDeviceList allDevices = UsbDevice.AllDevices; foreach (UsbRegistry usbRegistry in allDevices) { if (usbRegistry.Open(out MyUsbDevice)) { Console.WriteLine(MyUsbDevice.Info.ToString()); for (int iConfig = 0; iConfig < MyUsbDevice.Configs.Count; iConfig++) { UsbConfigInfo configInfo = MyUsbDevice.Configs[iConfig]; Console.WriteLine(configInfo.ToString()); ReadOnlyCollection<UsbInterfaceInfo> interfaceList = configInfo.InterfaceInfoList; for (int iInterface = 0; iInterface < interfaceList.Count; iInterface++) { UsbInterfaceInfo interfaceInfo = interfaceList[iInterface]; Console.WriteLine(interfaceInfo.ToString()); ReadOnlyCollection<UsbEndpointInfo> endpointList = interfaceInfo.EndpointInfoList; for (int iEndpoint = 0; iEndpoint < endpointList.Count; iEndpoint++) { Console.WriteLine(endpointList[iEndpoint].ToString()); } } } } } // Free usb resources. // This is necessary for libusb-1.0 and Linux compatibility. UsbDevice.Exit(); // Wait for user input.. Console.ReadKey(); } } }
Example
CopyVB.NET
Imports System Imports LibUsbDotNet Imports LibUsbDotNet.Info Imports LibUsbDotNet.Main Imports System.Collections.ObjectModel Namespace Examples Friend Class ShowInfo Public Shared MyUsbDevice As UsbDevice Public Shared Sub Main(args As String()) ' Dump all devices and descriptor information to console output. Dim allDevices As UsbRegDeviceList = UsbDevice.AllDevices For Each usbRegistry As UsbRegistry In allDevices If usbRegistry.Open(MyUsbDevice) Then Console.WriteLine(MyUsbDevice.Info.ToString()) For iConfig As Integer = 0 To MyUsbDevice.Configs.Count - 1 Dim configInfo As UsbConfigInfo = MyUsbDevice.Configs(iConfig) Console.WriteLine(configInfo.ToString()) Dim interfaceList As ReadOnlyCollection(Of UsbInterfaceInfo) = configInfo.InterfaceInfoList For iInterface As Integer = 0 To interfaceList.Count - 1 Dim interfaceInfo As UsbInterfaceInfo = interfaceList(iInterface) Console.WriteLine(interfaceInfo.ToString()) Dim endpointList As ReadOnlyCollection(Of UsbEndpointInfo) = interfaceInfo.EndpointInfoList For iEndpoint As Integer = 0 To endpointList.Count - 1 Console.WriteLine(endpointList(iEndpoint).ToString()) Next Next Next End If Next ' Free usb resources. ' This is necessary for libusb-1.0 and Linux compatibility. UsbDevice.[Exit]() ' Wait for user input.. Console.ReadKey() End Sub End Class End Namespace
Compiling the Code
Create a new console application in your favorite designer.
Verify your project references:
System.dll
LibUsbDotNet.dll
Add/edit the main class. Copy/Paste code from one of the examples above.
Robust Programming
The LastErrorNumber
and LastErrorString
properties should be avoided in production code. For robust error handling, use the
UsbErrorEvent event.
The event provides more information, it is executed for every error, and allows
some errors to be handled.