簡介:在串口通信的程序中,如果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 就可以了

