我想发现没有。连接到计算机的鼠标。但目前下面的代码(参考:Get List of connected USB Devices)检测到连接的USB鼠标和键盘(可能是任何USB输入设备/复合设备)。
但我只想在程序中检测鼠标。
我该怎么做。
非常感谢您的帮助。
提前致谢。
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
namespace ConsoleApplication2_0
{
class Program
{
static void Main(string[] args)
{
var usbDevices = GetUSBDevices();
Console.WriteLine("Total USB Mouse : " + usbDevices.Count);
Console.Read();
}
static List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
collection = searcher.Get();
foreach (var device in collection)
{
Console.WriteLine("-----------------------------------------------------");
//if ((string)device.GetPropertyValue("Description") == "USB Mass Storage Device")
if ((string)device.GetPropertyValue("Description") == "USB Composite Device")
{
Console.WriteLine("__RELPATH\t" + device.GetPropertyValue("__RELPATH").ToString());
foreach (var mouseprop in device.Properties)
{
if (mouseprop.Value != null)
Console.WriteLine(mouseprop.Name.ToString() + "\t" + mouseprop.Value.ToString());
}
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
));
}
}
collection.Dispose();
return devices;
}
}
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
}
public string DeviceID { get; private set; }
public string PnpDeviceID { get; private set; }
public string Description { get; private set; }
}
}