コード例 #1
0
        public static bool CheckDA2Adapter()
        {
            bool                     result          = false;
            ObjectQuery              objectQuery     = new ObjectQuery("SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0 and Caption like 'ITL USB (High Speed)%'");
            ConnectionOptions        options         = ProcessConnection.ProcessConnectionOptions();
            ManagementScope          connectionScope = ProcessConnection.ConnectionScope(Environment.MachineName, options, @"\root\CIMV2");
            ManagementObjectSearcher comPortSearcher = new ManagementObjectSearcher(connectionScope, objectQuery);

            try
            {
                using (comPortSearcher)
                {
                    foreach (ManagementObject obj in comPortSearcher.Get())
                    {
                        if (obj != null)
                        {
                            result = true;
                            break;
                        }
                    }
                }
            }
            catch
            {
            }

            return(result);
        }
コード例 #2
0
        private static void GetEmulatorComPort()
        {
            ObjectQuery              objectQuery     = new ObjectQuery("SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0 and Caption like 'ELTIMA Virtual%'");
            ConnectionOptions        options         = ProcessConnection.ProcessConnectionOptions();
            ManagementScope          connectionScope = ProcessConnection.ConnectionScope(Environment.MachineName, options, @"\root\CIMV2");
            ManagementObjectSearcher comPortSearcher = new ManagementObjectSearcher(connectionScope, objectQuery);

            try
            {
                using (comPortSearcher)
                {
                    string caption = null;
                    foreach (ManagementObject obj in comPortSearcher.Get())
                    {
                        if (obj != null)
                        {
                            object captionObj = obj["Caption"];
                            if (captionObj != null)
                            {
                                caption = captionObj.ToString();
                                if (caption.Contains("(COM"))
                                {
                                    com_port = caption.Substring(caption.LastIndexOf("(COM")).Replace("(", string.Empty).Replace(")", string.Empty);
                                    int pos = com_port.IndexOf("->");
                                    if (pos > 0)
                                    {
                                        com_port = com_port.Substring(0, pos);
                                        ComPorts.Add(com_port);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
            }
        }