鸡蛋西红柿CC

导航

获取设备管理器端口号

static void Main(string[] args)
        {

            List<string> vaildportname = new List<string>();
            foreach (string portname in SerialPort.GetPortNames())
            {
                bool vaild = true;
                SerialPort vaildport = new SerialPort(portname);
                try
                {
                    vaildport.Open();
                }
                catch
                {
                    vaild = false;
                }
                finally
                {
                    vaildport.Close();
                }
                if (vaild == true)
                {
                    vaildportname.Add(portname);
                }
            }       
            foreach (string s in vaildportname)
            {
                Console.WriteLine(s);
            }
            Console.ReadKey();
        }

 

posted on 2013-11-22 10:21  鸡蛋西红柿CC  阅读(484)  评论(0编辑  收藏  举报