How get the serial port name of an Arduino Leonardo

I'm trying to figure out which serial port belongs to an Arduino Leonardo. Since the SerialPort class does not reveal any information about the underlying hardware, I'm trying to use LibUsbDotNet instead.

Using the following code to get a list of all devices:

UsbDevice.ForceLibUsbWinBack = true; var devices = UsbDevice.AllDevices;

I can then either iterate over all of them or search for the Arduino specifically:

var deviceFinder = new UsbDeviceFinder(0x2341, 0x8036); var arduino = devices.Find(deviceFinder);

This actually works and I get an instance of UsbRegistry, but I'm unable to open the device or determine through which serial port it is exposed.

USBDevice arduinoUsbDevice; usbRegistry.Open(out arduinoUsbDevice);

Since this doesn't work arduinoUsbDevice remains null.

I then tried using the DeviceNotifier class which raises an event whenever a device is added or removed from the system:

var notifier = DeviceNotifier.OpenDeviceNotifier();

notifier.OnDeviceNotify += (s, e) =>
{
    WriteLine(e.Device?.Name ?? "no device");
    WriteLine(e.Device?.IdProduct ?? 0);
    WriteLine(e.Device?.IdVendor ?? 0);
    WriteLine(e.EventType);
    WriteLine(e.Object);
    WriteLine(e.Port?.Name ?? "");
};

Now whenever I connect the Arduino Leonardo to the computer, the event is raised twice. As if two separate devices are being connected, but only one of them is ever returned by UsbDevice.AllDevices:

` \\?\USB#VID_2341&PID_8036#7&533912d&0&2#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
32822
9025
DeviceArrival
FullName:USB#VID_2341&PID_8036#7&533912d&0&2#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
Vid:0x2341
Pid:0x8036
SerialNumber:7&533912d&0&2
ClassGuid:a5dcbf10-6530-11d2-901f-00c04fb951ed


no device
0
0
DeviceArrival
[Port Name:COM5] 
COM5

The first time the event is raised for the device we could find before as well. The second time it is raised with e.Device set to null but with e.Port set to COM5, which is the information I'm after.

So the problem is I can only get this information when the Arduino Leonardo is connected after the software has been started and even then linking the two events is kind of a guessing game.

Is there any way of getting the information without having to rely on the events raised by the DeviceNotifier class?

I'm aware I could use System.Management and WMI queries, but these are not available on Linux and MacOS, which is why I'm using LibUsbDotNet instead.

The native library I'm using is libusb-1.0

JO3RI: Now whenever I connect the Arduino Leonardo to the computer, the event is raised twice.

The first one might be the bootloader.