如何确定我的电脑附带的设备名称?

时间:2015-05-20 09:10:57

标签: c# wpf

我的PC附带了两个设备(Genovation Keypad和FUJITSU掌上扫描仪)。如果我从其中任何一个获取输入,那么如何确定哪个设备给我输入?

我已经使用这段代码确认设备是否与PC连接。

C#代码:

using System;
using System.Collections.Generic;
using System.Management; // need to add System.Management to your project references.

class Program
{
    static void Main(string[] args)
    {
        var usbDevices = GetUSBDevices();

        string text = "";
        foreach (var usbDevice in usbDevices)
        {
            text += string.Format("Device ID: {0}, PNP Device ID: {1}, Description: {2}",
                usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description) + Environment.NewLine;
        }

        System.IO.File.WriteAllText(@"d:\Rough\AllConnectedDevices.txt", text);

    }

    static List<USBDeviceInfo> GetUSBDevices()
    {
        List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

        ManagementObjectCollection collection;

        using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity"))
            collection = searcher.Get();

        foreach (var device in collection)
        {
            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; }
}

输出: 设备ID:HID \ VID_04B4&amp; PID_0100 \ 6&amp; 1474E62E&amp; 1&amp; 0000,PNP设备ID:HID \ VID_04B4&amp; PID_0100 \ 6&amp; 1474E62E&amp; 1&amp; 0000,描述:HID键盘设备

设备ID:USB \ VID_04C5&amp; PID_1084 \ 5&amp; 36C74247&amp; 0&amp; 3,PNP设备ID:USB \ VID_04C5&amp; PID_1084 \ 5&amp; 36C74247&amp; 0&amp; 3,描述:FUJITSU PalmSecure传感器设备

0 个答案:

没有答案