public static List <Win32PnPEntityClass> GetWin32PnPEntityClassDevices(string condition)
        {
            List <Win32PnPEntityClass> result = new List <Win32PnPEntityClass>();

            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_PnPEntity where " + condition))
            {
                foreach (var o in searcher.Get())
                {
                    var managementObject         = (ManagementObject)o;
                    Win32PnPEntityClass portInfo = new Win32PnPEntityClass();
                    MapObject2Win32PnPEntityClass(managementObject, portInfo);
                    if (!portInfo.Caption.Contains("LPT"))
                    {
                        result.Add(portInfo);
                    }
                }
            }
            return(result);
        }
        public static List <Win32PnPEntityClass> GetWin32PnPEntityClassSerialPortDevices()
        {
            List <Win32PnPEntityClass> result = new List <Win32PnPEntityClass>();

            _serialPortNames = new List <string>(GetDeviceNames());
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_PnPEntity where ClassGuid = '{4D36E978-E325-11CE-BFC1-08002BE10318}' and Service <> 'Parport' "))
            {
                foreach (var o in searcher.Get())
                {
                    var managementObject         = (ManagementObject)o;
                    Win32PnPEntityClass portInfo = new Win32PnPEntityClass();

                    MapObject2Win32PnPEntityClass(managementObject, portInfo);
                    if (!portInfo.Caption.Contains("LPT"))
                    {
                        result.Add(portInfo);
                    }
                }
            }
            //foreach (string sPortName in serialPortNames)
            //{
            //    bool isCreated = false;
            //    foreach (Win32PnPEntityClass pInfo in result)
            //    {
            //        if (pInfo.DeviceID == sPortName)
            //        {
            //            isCreated = true;
            //            break;
            //        }
            //    }
            //    if (!isCreated)
            //    {
            //        Win32PnPEntityClass _pInfo = new Win32PnPEntityClass();
            //        _pInfo.DeviceID = sPortName;
            //        _pInfo.Caption = sPortName;

            //        result.Add(_pInfo);
            //    }
            //}
            return(result);
        }
        public static List <Win32PnPEntityClass> GetWin32PnPEntityClassSerialPortDevices(string condition)
        {
            List <Win32PnPEntityClass> result = new List <Win32PnPEntityClass>();

            if (!condition.Trim().ToUpper().StartsWith("AND"))
            {
                condition = " and " + condition;
            }
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_PnPEntity where ClassGuid = '{4D36E978-E325-11CE-BFC1-08002BE10318}' and Service <> 'Parport' " + condition))
            {
                foreach (var o in searcher.Get())
                {
                    var managementObject         = (ManagementObject)o;
                    Win32PnPEntityClass portInfo = new Win32PnPEntityClass();
                    MapObject2Win32PnPEntityClass(managementObject, portInfo);
                    if (!portInfo.Caption.Contains("LPT"))
                    {
                        result.Add(portInfo);
                    }
                }
            }
            return(result);
        }
        private static void MapObject2Win32PnPEntityClass(ManagementBaseObject managementObject, Win32PnPEntityClass portInfo)
        {
            MapObject2BaseWin32DeviceClass(managementObject, portInfo);
            if (managementObject.Properties["ClassGuid"].Value != null)
            {
                portInfo.ClassGuid = (string)managementObject.Properties["ClassGuid"].Value;
            }
            if (managementObject.Properties["ErrorCleared"].Value != null)
            {
                portInfo.ErrorCleared = (bool?)managementObject.Properties["ErrorCleared"].Value;
            }
            if (managementObject.Properties["ErrorDescription"].Value != null)
            {
                portInfo.ErrorDescription = (string)managementObject.Properties["ErrorDescription"].Value;
            }
            if (managementObject.Properties["InstallDate"].Value != null)
            {
                portInfo.InstallDate = (DateTime?)managementObject.Properties["InstallDate"].Value;
            }
            if (managementObject.Properties["LastErrorCode"].Value != null)
            {
                portInfo.LastErrorCode = (int?)managementObject.Properties["LastErrorCode"].Value;
            }
            if (managementObject.Properties["Manufacturer"].Value != null)
            {
                portInfo.Manufacturer = (string)managementObject.Properties["Manufacturer"].Value;
            }
            if (managementObject.Properties["Service"].Value != null)
            {
                portInfo.Service = (string)managementObject.Properties["Service"].Value;
            }

            if (!string.IsNullOrEmpty(portInfo.Caption))
            {
                foreach (string s in _serialPortNames)
                {
                    if (portInfo.Caption.Contains("(" + s + ")"))
                    {
                        portInfo.DeviceId = s;
                    }
                }
            }
        }