Search code examples
c#sharppcap

How to access friendly name of device in SharpPCap


I want to access the friendly name of network devices in C# using the SharpPCap library. I get a list of devices with CaptureDeviceList deviceList = CaptureDeviceList.Instance; and loop through it with a foreach to access each one:

foreach (ICaptureDevice device in deviceList)
{
    Console.WriteLine($"{device.Name}--{device.Description}");
}

The PCapInterface seemed to contain the FriendlyName property so I tried casting it to that type but it did not work and returned a null value.


Solution

  • So basically you have to approach this differently. You should get the list of LibPcapLiveDevice instead of ICaptureDevice using LibPcapLiveDeviceList.Instance. You can then iterate through that list, access the Interface property and then access its FriendlyName property, and you can implicitly cast the LibPcapLiveDevice to an ICaptureDevice because it inherits from it. Example code:

    LibPcapLiveDeviceList deviceList = LibPcapLiveDeviceList.Instance;
    ICaptureDevice device = null!;
    
    foreach (LibPcapLiveDevice liveDevice in deviceList)
    {
        if (liveDevice.Interface.FriendlyName == "WLAN")
        {
            device = liveDevice;
            break;
        }
    }