Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Getting a list of names of the webcams

I have searched for several days to no avail. I am trying to simply list to a text file the Image Device Names i.e. webcams using c#. I know I can use System.IO.Ports to get comports, which I am doing, but I cannot find a simple way to list the Image Devices.

I have been able to find the WIA devices with this code but not non-WIA Devices:

    private static void DoWork()
    {
        var deviceManager1 = new DeviceManager();
        for (int i = 1; (i <= deviceManager1.DeviceInfos.Count); i++)
        {
           // if (deviceManager1.DeviceInfos[i].Type !=   
     WiaDeviceType.VideoDeviceType) { continue; }


     Console.WriteLine(deviceManager1.DeviceInfos[i].
     Properties["Name"].get_Value().  ToString());
     }
like image 204
Brad Jarrett Avatar asked Oct 28 '25 10:10

Brad Jarrett


1 Answers

As I answered in this question, you can do it without external libraries by using WMI.

Add using System.Management; and then:

public static List<string> GetAllConnectedCameras()
{
    var cameraNames = new List<string>();
    using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE (PNPClass = 'Image' OR PNPClass = 'Camera')"))
    {
        foreach (var device in searcher.Get())
        {
            cameraNames.Add(device["Caption"].ToString());
        }
    }

    return cameraNames;
}
like image 53
Francesco Bonizzi Avatar answered Oct 30 '25 01:10

Francesco Bonizzi