C#获取网络摄像头的名称列表

时间:2017-03-07 18:45:49

标签: c# webcam system.io.packaging

我搜索了几天无济于事。我试图简单地将文件文件列入图像设备名称,即使用c#的网络摄像头。我知道我可以使用System.IO.Ports来获取我正在进行的编译,但我找不到一种简单的方法来列出图像设备。

我已经能够找到带有此代码的WIA设备,但不能找到非WIA设备:

    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());
     }

2 个答案:

答案 0 :(得分:1)

当我回答in this question时,您可以使用WMI在没有外部库的情况下进行操作。

添加using System.Management;,然后:

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;
}

答案 1 :(得分:0)

我有几条路线供您查看。

尝试添加对Interop.WIA.dll(Microsoft Windows Image Acquisition Library)的引用,并使用以下代码枚举设备。然后,您可以使用设备属性过滤相关的。

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());
            }
        }
    }
}

}

如果这不起作用,您可以尝试使用DEVCON,这是一个Microsoft工具。 DEVCON允许从命令行进行设备管理。您可以尝试使用适当的标志调用它并读取输出。 (http://www.robvanderwoude.com/devcon.php

相关问题