Scanning for a Human Interface Device (HID) using C#

后端 未结 2 643
忘了有多久
忘了有多久 2021-01-13 07:31

I am developing a C# .NET 2.0 application wherein I need to scan for an attached HID. How can this be done? Because it is a HID, Windows does not assign a COM port to it.

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-13 07:57

    In the WMI Code Creator select these options:

    Namespace: root\WMI

    Class: MSWmi_PnPInstanceNames

    Select InstanceNames from the Results box for the following code:

    using System;
    using System.Management;
    using System.Windows.Forms;
    
    namespace WMISample
    {
        public class MyWMIQuery
        {
            public static void Main()
            {
                try
                {
                    ManagementObjectSearcher searcher = 
                        new ManagementObjectSearcher("root\\WMI", 
                        "SELECT * FROM MSWmi_PnPInstanceNames"); 
    
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("MSWmi_PnPInstanceNames instance");
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("InstanceName: {0}", queryObj["InstanceName"]);
                    }
                }
                catch (ManagementException e)
                {
                    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
                }
            }
        }
    }
    

提交回复
热议问题