I want to detect no. of mouse(s) connected to the computer. But currently the below code (reference:Get List of connected USB Devices) detects both connected USB mouse and Keyboard(May be any USB input device/composite device).
But I only want mouse to be detected by in program.
How do I do that.
Your help would be appreciated.
Thanks in advance.
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; }
}
}