2

How can I get a list all of camera devices attached to my PC using USB (WebCams) but also the build in camera that the laptops have.

chopper
  • 6,649
  • 7
  • 36
  • 53
Devsined
  • 3,363
  • 6
  • 30
  • 48

3 Answers3

13

A simple solution without any external library is to use 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;
}
Francesco Bonizzi
  • 5,142
  • 6
  • 49
  • 88
7

I've done this before - use http://directshownet.sourceforge.net/ to give you a decent .net interface to DirectShow, then you can just use the following code:

  DsDevice[] captureDevices;

  // Get the set of directshow devices that are video inputs.
  captureDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);    

  for (int idx = 0; idx < captureDevices.Length; idx++)
  {
    // Do something with the device here...
  }
Alistair Evans
  • 36,057
  • 7
  • 42
  • 54
0

Hope it helps other users

//using System.Management;
    public void GetCameras()
    {
        List<string> cameras = new List<string>();
        var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE (PNPClass = 'Image' OR PNPClass = 'Camera')");
        
        foreach (var device in searcher.Get())
        {
            cameras.Add($"Device: {device["PNPClass"]} / {device["Caption"]} / {device["Description"]} / {device["Manufacturer"]}");
        }
        
        File.WriteAllLines(@"C:\out\cameras.txt", cameras);
    }
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 29 '22 at 00:19
  • Welcome to Stackoverflow. This question is asked more than 9 years ago and it has an accepted answer. Please add some details about the reason you are adding a new answer. – MD Zand Nov 30 '22 at 12:57