c#获取本机串口列表

private List GetComlist(bool isUseReg)
        {
            List list = new List();
            try
            {
                if (isUseReg)
                {
                    RegistryKey RootKey = Registry.LocalMachine;
                    RegistryKey Comkey = RootKey.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM");

                    String[] ComNames = Comkey.GetValueNames();

                    foreach (String ComNamekey in ComNames)
                    {
                        string TemS = Comkey.GetValue(ComNamekey).ToString();
                        list.Add(TemS);
                    }
                }
                else
                {
                    foreach (string com in System.IO.Ports.SerialPort.GetPortNames())  //自动获取串行口名称
                        list.Add(com);
                }
            }
            catch
            {
            }
            return list;
        }
以上两种方式,一种是直接用IO的方式,一种是用注册表的方式,两种获取速度都还不错

你可能感兴趣的:(C#)