Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用WMI查询识别给定USB VID和PID的USB到串行端口_C#_Wmi Query_Usbserial - Fatal编程技术网

C# 使用WMI查询识别给定USB VID和PID的USB到串行端口

C# 使用WMI查询识别给定USB VID和PID的USB到串行端口,c#,wmi-query,usbserial,C#,Wmi Query,Usbserial,我使用WMI查询来检测USB到串行端口,但问题是,在windows 7中,应用程序需要很长时间才能启动,而在windows xp中,它工作正常。我以以下方式使用wmi查询 ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPDevice"); foreach (ManagementObject queryObj in

我使用WMI查询来检测USB到串行端口,但问题是,在windows 7中,应用程序需要很长时间才能启动,而在windows xp中,它工作正常。我以以下方式使用wmi查询

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPDevice");
            foreach (ManagementObject queryObj in searcher.Get())
            {
                if (queryObj["SameElement"].ToString().Contains("SerialPort"))
                {
                    //do something
                 }
             }

根据我的推理,这是由于大量的Pnp设备和从该列表中查询串行端口造成的。我尝试使用Win32_SerialPort,但它在windows xp中工作,而在我的笔记本电脑(windows 7)上,它显示消息不受支持,即使它们是虚拟串行端口和USB到串行端口。它甚至不能从管理员帐户工作。MSSerial_PortName在我的笔记本电脑(windows7)上也不工作。因此,在windows 7中使用WMI查询是否可以让我的应用程序更快地启动?

Win32\p设备需要很长时间 MSSerial_PortName在我的Win 7笔记本电脑上工作。 试试这个(应该可以,从我的程序中修改):


Win32\u pDevice需要很长时间 MSSerial_PortName在我的Win 7笔记本电脑上工作。 试试这个(应该可以,从我的程序中修改):

try
    {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName");
        foreach (ManagementObject queryObj in searcher.Get())
        {
            serialPort = new SerialPort(queryObj["PortName"].ToString(), 115200, Parity.None, 8, StopBits.One);//change parameters
            //If the serial port's instance name contains USB
            //it must be a USB to serial device
            if (queryObj["InstanceName"].ToString().Contains("USB"))//if you have a VID or PID name then this should not be nessesery
            {
                //should get serial to usb adapters
                try
                {
                    serialPort.Open();
                    if (queryObj["InstanceName"].ToString().Contains(VID_or_PID))
                    {
                        //do sth
                    }
                    serialPort.Close();
                }
                catch (Exception ex)
                {
                    //exception handling
                }
            }

        }
    }
    catch (ManagementException ex)
    {
        //write("Querying for WMI data. Exception raised(" + ex.Message + "): " + ex);
    }