.net获取串口列表
来源:互联网 发布:mac智能文件夹怎么用 编辑:程序博客网 时间:2024/05/20 08:43
//方法一 String[] Str2 = System.IO.Ports.SerialPort.GetPortNames(); if (Str2.Length > 0) { for (int k = 0; k < Str2.Length; k++) { comboBoxEdit1.Properties.Items.Add(Str2[k]); } } //方法二 String[] Str2 = Gprs.GetSerialPortNames(); if (Str2.Length > 0) { for (int k = 0; k < Str2.Length; k++) { comboBoxEdit1.Properties.Items.Add(Str2[k]); } } //方法三 RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm"); if (keyCom != null) { string[] sSubKeys = keyCom.GetValueNames(); comboBoxEdit1.Properties.Items.Clear(); foreach (string sName in sSubKeys) { string sValue = (string)keyCom.GetValue(sName); comboBoxEdit1.Properties.Items.Add(sValue); } }
其中方法二的方法不是.net自带的需要自己写:
public string[] GetSerialPortNames() { string[] ports = null; List<string> portsList = new List<string>(); uint HKEY_LOCAL_MACHINE = 0x80000002; int hKey = -1; int ret = RegOpenKey(HKEY_LOCAL_MACHINE, @"Hardware\DEVICEMAP\SERIALCOMM", ref hKey); try { if (ret == 0) { int index = 0; int BufferSize = 255; int ERROR_NO_MORE_ITEMS = 259; string valueName = "".PadRight(BufferSize, ' '); int valueNameLength = BufferSize; int valueLength = BufferSize; string value = "".PadRight(BufferSize, ' '); while (RegEnumValue(hKey, index, ref valueName, ref valueNameLength, 0, 0, ref value, ref valueLength) != ERROR_NO_MORE_ITEMS) { if (valueLength > 0) { if (value[valueLength - 1] == 0) valueLength -= 1; portsList.Add(value.Substring(0, valueLength)); } index += 1; valueName = "".PadRight(BufferSize, ' '); valueNameLength = BufferSize; valueLength = BufferSize; } } } catch (Exception) { } finally { if (ret == 0) RegCloseKey(hKey); } if (portsList.Count == 0) ports = new string[0]; else ports = portsList.ToArray(); return ports; }
这里的三个方法第一个使用的是.net自带的类进行函数调用,第二个是使用win32 的api方法,第三个是读取注册表信息。
- .net获取串口列表
- C#获取串口列表
- C#获取串口列表
- delphi 获取串口列表
- 获取系统可用串口列表
- .net获取文件列表
- c#获取本机串口列表
- c#获取本机的串口列表
- C#获取串口列表(可用于实现实时监控串口)
- .net 获取odbc驱动列表
- vb.net 获取进程列表
- 获取串口
- VC++ 枚举串口列表
- windows取得串口列表
- 注册表查看串口列表
- 在.net平台中 获取ODBC数据对象的列表
- .Net获取局域网中可以ping通的IP列表
- asp.net一般处理程序(2)获取列表
- 位运算简介及实用技巧(一):基础篇
- leetcode spiral matrix
- DataGridView 列合并
- 找个属于自己的路
- HDU1874:畅通工程续
- .net获取串口列表
- leetcode Jump Game
- Android服务启动之StartService源码分析
- SQL Server 2000安装遇到的问题
- VirtualBox Ubuntu 12.04 Access Windows 7 File System
- Codeforces---Lucky Conversion
- 安卓应用程序无反应分析
- sql常用语句
- 集训两次周赛总结