c#获取本机串口列表

来源:互联网 发布:微信矩阵如何做 编辑:程序博客网 时间:2024/05/09 07:33
private List<string> GetComlist(bool isUseReg)        {            List<string> list = new List<string>();            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的方式,一种是用注册表的方式,两种获取速度都还不错
0 0
原创粉丝点击