4

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());
     }
Brad Jarrett
  • 109
  • 3
  • 11

2 Answers2

3

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;
}
Francesco Bonizzi
  • 5,142
  • 6
  • 49
  • 88
-1

I have a couple routes for you to check out.

Try adding a reference to Interop.WIA.dll (Microsoft Windows Image Acquisition Library) and using the following code to enumerate the devices. You can then filter for the relevant ones using a device property.

using System;
using WIA;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        DoWork();
        Console.ReadKey();
    }

    private static void DoWork()
    {
        var deviceManager1 = new DeviceManager();
        for (int i = 1; (i <= deviceManager1.DeviceInfos.Count); i++)
        {
            if (deviceManager1.DeviceInfos[i].Type == WiaDeviceType.CameraDeviceType|| deviceManager1.DeviceInfos[i].Type == WiaDeviceType.VideoDeviceType)
            {
                Console.WriteLine(deviceManager1.DeviceInfos[i].Properties["Name"].get_Value().ToString());
            }
        }
    }
}

}

If that doesn't work, you can always try using DEVCON, which is a Microsoft tool. DEVCON allows device management from the command line. You could try invoking it with the appropriate flags and reading the output. (http://www.robvanderwoude.com/devcon.php)

Jon S
  • 158
  • 9
  • Thanks Jon....geting some lbxDevices errors..working through those to see if this can work.... – Brad Jarrett Mar 07 '17 at 20:11
  • lbxDevices is a list of devices that I was populating. You should be able to sub that out for whatever else you're doing. The salient part is the rest of it which populates that list. – Jon S Mar 07 '17 at 20:21
  • Sorry, I am new at C#...I just want to list them in the Console. – Brad Jarrett Mar 07 '17 at 20:29
  • @BradJarrett, I updated the code. That's the whole program that outputs the names of all COM objects. You can then filter based on properties of each DeviceInfo. For example, you can `if (deviceManager1.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType) { continue; }` – Jon S Mar 07 '17 at 21:10
  • @ Jon S, it works as desired but only getting first two devices in the Imaging Devices...There are 4 in the Imaging Device area alone. – Brad Jarrett Mar 07 '17 at 21:32
  • @ Jon S, after some more testing...I had two scanner/printers and two webcams in the Image Devices...It was only showing the two scanner/printers. I deleted one of them and re-ran the console and it only shows the 1 scanner...so it is only finding the scanners. – Brad Jarrett Mar 07 '17 at 21:35
  • @ Jon S, my guess is the two cameras are not using WIA drivers.....thoughts on how to read those? – Brad Jarrett Mar 07 '17 at 21:48
  • Just a guess, but they might use TWAIN. I found a good link http://www.dynamsoft.com/blog/document-imaging/fingerprint-scanner-csharp/. There's a link to a download on that page that checks if your imaging device is TWAIN-compliant. I don't have much experience with it though. – Jon S Mar 07 '17 at 21:58
  • Ok, thanks...I was looking at the device manager details...and the scanner does have WIA drivers...the cameras just say USB Video Device...I need to figure out how to scan for those. Thanks for the help! – Brad Jarrett Mar 07 '17 at 22:11