[英]C# Getting a list of names of the webcams
我找了好幾天都沒有用。 我試圖簡單地將圖像設備名稱(即使用 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());
}
正如我在這個問題中回答的那樣,您可以使用 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;
}
我有幾條路線供您查看。
嘗試添加對 Interop.WIA.dll(Microsoft Windows 圖像采集庫)的引用並使用以下代碼枚舉設備。 然后,您可以使用設備屬性篩選相關的。
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());
}
}
}
}
}
如果這不起作用,您可以隨時嘗試使用 Microsoft 工具 DEVCON。 DEVCON 允許從命令行管理設備。 您可以嘗試使用適當的標志調用它並讀取輸出。 ( http://www.robvanderwoude.com/devcon.php )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.