简介:在串口通信的程序中,如果PC上同时连接有多个串口,那么从应用程序里打开串口时,就很难知道是哪一个串口,这时候就必须要通过设备管理器去查看串口名称,这份代码就是解决这个问题,调用系统api,读取串口设备的名称和串口号,不需要再从设备管理器里去查找串口。程序关键部分如下:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Management;
6
7 namespace StudentSerialPort
8 {
9 class GetHardName
10 {
11 /// <summary>
12 /// 枚举win32 api
13 /// </summary>
14 public enum HardwareEnum
15 {
16 // 硬件
17 Win32_Processor, // CPU 处理器
18 Win32_PhysicalMemory, // 物理内存条
19 Win32_Keyboard, // 键盘
20 Win32_PointingDevice, // 点输入设备,包括鼠标。
21 Win32_FloppyDrive, // 软盘驱动器
22 Win32_DiskDrive, // 硬盘驱动器
23 Win32_CDROMDrive, // 光盘驱动器
24 Win32_BaseBoard, // 主板
25 Win32_BIOS, // BIOS 芯片
26 Win32_ParallelPort, // 并口
27 Win32_SerialPort, // 串口
28 Win32_SerialPortConfiguration, // 串口配置
29 Win32_SoundDevice, // 多媒体设置,一般指声卡。
30 Win32_SystemSlot, // 主板插槽 (ISA & PCI & AGP)
31 Win32_USBController, // USB 控制器
32 Win32_NetworkAdapter, // 网络适配器
33 Win32_NetworkAdapterConfiguration, // 网络适配器设置
34 Win32_Printer, // 打印机
35 Win32_PrinterConfiguration, // 打印机设置
36 Win32_PrintJob, // 打印机任务
37 Win32_TCPIPPrinterPort, // 打印机端口
38 Win32_POTSModem, // MODEM
39 Win32_POTSModemToSerialPort, // MODEM 端口
40 Win32_DesktopMonitor, // 显示器
41 Win32_DisplayConfiguration, // 显卡
42 Win32_DisplayControllerConfiguration, // 显卡设置
43 Win32_VideoController, // 显卡细节。
44 Win32_VideoSettings, // 显卡支持的显示模式。
45
46 // 操作系统
47 Win32_TimeZone, // 时区
48 Win32_SystemDriver, // 驱动程序
49 Win32_DiskPartition, // 磁盘分区
50 Win32_LogicalDisk, // 逻辑磁盘
51 Win32_LogicalDiskToPartition, // 逻辑磁盘所在分区及始末位置。
52 Win32_LogicalMemoryConfiguration, // 逻辑内存配置
53 Win32_PageFile, // 系统页文件信息
54 Win32_PageFileSetting, // 页文件设置
55 Win32_BootConfiguration, // 系统启动配置
56 Win32_ComputerSystem, // 计算机信息简要
57 Win32_OperatingSystem, // 操作系统信息
58 Win32_StartupCommand, // 系统自动启动程序
59 Win32_Service, // 系统安装的服务
60 Win32_Group, // 系统管理组
61 Win32_GroupUser, // 系统组帐号
62 Win32_UserAccount, // 用户帐号
63 Win32_Process, // 系统进程
64 Win32_Thread, // 系统线程
65 Win32_Share, // 共享
66 Win32_NetworkClient, // 已安装的网络客户端
67 Win32_NetworkProtocol, // 已安装的网络协议
68 Win32_PnPEntity,//all device
69 }
70 /// <summary>
71 /// WMI取硬件信息
72 /// </summary>
73 /// <param name="hardType"></param>
74 /// <param name="propKey"></param>
75 /// <returns></returns>
76 public static string[] MulGetHardwareInfo(HardwareEnum hardType, string propKey)
77 {
78
79 List<string> strs = new List<string>();
80 try
81 {
82 using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + hardType))
83 {
84 var hardInfos = searcher.Get();
85 foreach (var hardInfo in hardInfos)
86 {
87 if(hardInfo.Properties[propKey].Value != null)
88 {
89 if (hardInfo.Properties[propKey].Value.ToString().Contains("COM"))
90 {
91 strs.Add(hardInfo.Properties[propKey].Value.ToString());
92 }
93 }
94
95
96 }
97 searcher.Dispose();
98 }
99 return strs.ToArray();
100 }
101 catch
102 {
103 return null;
104 }
105 finally
106 { strs = null; }
107 }
108 //通过WMI获取COM端口
109 /// <summary>
110 /// 串口信息
111 /// </summary>
112 /// <returns></returns>
113 public static string[] GetSerialPort()
114 {
115 return MulGetHardwareInfo(HardwareEnum.Win32_PnPEntity, "Name");
116 }
117
118
119
120 }
121 }
使用方式:调用API "GetSerialPort"即可获得设备列表,示意如下:
1 //通过WMI获取COM端口
2 foreach (string portName in GetHardName.GetSerialPort())
3 {
4 toolStripItemCollection.Add(portName);
5 }
效果如图:

如果报错:
managementobjectsearcher 缺少using
为什么已经引用了using System.Management 使用ManagementObjectSearcher时为什么提示未引用空间
解决办法:
在项目》》添加引用....里面引用System.Management 。 再using System.Management 就可以了
