主要针对当前的usb转串口进行了穷举。
方便判断串口对应哪个设备。
返回串口名称
类对象,(包含了参考网址,以及对其进行了修改,防止出现蓝牙端口)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;using System.Management;namespace test
{//参考地址 https://www.cnblogs.com/weiterli/p/8260790.htmlinternal class GatSerialPortName{public enum HardwareEnum{// 硬件Win32_Processor, // CPU 处理器Win32_PhysicalMemory, // 物理内存条Win32_Keyboard, // 键盘Win32_PointingDevice, // 点输入设备,包括鼠标。Win32_FloppyDrive, // 软盘驱动器Win32_DiskDrive, // 硬盘驱动器Win32_CDROMDrive, // 光盘驱动器Win32_BaseBoard, // 主板Win32_BIOS, // BIOS 芯片Win32_ParallelPort, // 并口Win32_SerialPort, // 串口Win32_SerialPortConfiguration, // 串口配置Win32_SoundDevice, // 多媒体设置,一般指声卡。Win32_SystemSlot, // 主板插槽 (ISA & PCI & AGP)Win32_USBController, // USB 控制器Win32_NetworkAdapter, // 网络适配器Win32_NetworkAdapterConfiguration, // 网络适配器设置Win32_Printer, // 打印机Win32_PrinterConfiguration, // 打印机设置Win32_PrintJob, // 打印机任务Win32_TCPIPPrinterPort, // 打印机端口Win32_POTSModem, // MODEMWin32_POTSModemToSerialPort, // MODEM 端口Win32_DesktopMonitor, // 显示器Win32_DisplayConfiguration, // 显卡Win32_DisplayControllerConfiguration, // 显卡设置Win32_VideoController, // 显卡细节。Win32_VideoSettings, // 显卡支持的显示模式。// 操作系统Win32_TimeZone, // 时区Win32_SystemDriver, // 驱动程序Win32_DiskPartition, // 磁盘分区Win32_LogicalDisk, // 逻辑磁盘Win32_LogicalDiskToPartition, // 逻辑磁盘所在分区及始末位置。Win32_LogicalMemoryConfiguration, // 逻辑内存配置Win32_PageFile, // 系统页文件信息Win32_PageFileSetting, // 页文件设置Win32_BootConfiguration, // 系统启动配置Win32_ComputerSystem, // 计算机信息简要Win32_OperatingSystem, // 操作系统信息Win32_StartupCommand, // 系统自动启动程序Win32_Service, // 系统安装的服务Win32_Group, // 系统管理组Win32_GroupUser, // 系统组帐号Win32_UserAccount, // 用户帐号Win32_Process, // 系统进程Win32_Thread, // 系统线程Win32_Share, // 共享Win32_NetworkClient, // 已安装的网络客户端Win32_NetworkProtocol, // 已安装的网络协议Win32_PnPEntity,//all device}/// /// WMI取硬件信息/// /// /// /// public static string[] MulGetHardwareInfo(HardwareEnum hardType, string propKey){List strs = new List();try{//如果报错://managementobjectsearcher 缺少using//为什么已经引用了using System.Management 使用ManagementObjectSearcher时为什么提示未引用空间//解决办法://在项目》》添加引用....里面引用System.Management 。 再using System.Management 就可以了//此处需要安装扩展包using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + hardType)){var hardInfos = searcher.Get();foreach (var hardInfo in hardInfos){if (hardInfo.Properties[propKey].Value != null){// COMM 这样的蓝牙接口无法滤除//if (hardInfo.Properties[propKey].Value.ToString().Contains("COM"))//{// strs.Add(hardInfo.Properties[propKey].Value.ToString());//}if (hardInfo.Properties[propKey].Value.ToString().Contains("COM1")){strs.Add(hardInfo.Properties[propKey].Value.ToString());}else if (hardInfo.Properties[propKey].Value.ToString().Contains("COM2")){strs.Add(hardInfo.Properties[propKey].Value.ToString());}else if (hardInfo.Properties[propKey].Value.ToString().Contains("COM3")){strs.Add(hardInfo.Properties[propKey].Value.ToString());}else if (hardInfo.Properties[propKey].Value.ToString().Contains("COM4")){strs.Add(hardInfo.Properties[propKey].Value.ToString());}else if (hardInfo.Properties[propKey].Value.ToString().Contains("COM5")){strs.Add(hardInfo.Properties[propKey].Value.ToString());}else if (hardInfo.Properties[propKey].Value.ToString().Contains("COM6")){strs.Add(hardInfo.Properties[propKey].Value.ToString());}else if (hardInfo.Properties[propKey].Value.ToString().Contains("COM7")){strs.Add(hardInfo.Properties[propKey].Value.ToString());}else if (hardInfo.Properties[propKey].Value.ToString().Contains("COM8")){strs.Add(hardInfo.Properties[propKey].Value.ToString());}else if (hardInfo.Properties[propKey].Value.ToString().Contains("COM9")){strs.Add(hardInfo.Properties[propKey].Value.ToString());}}}searcher.Dispose();}return strs.ToArray();}catch{return null;}finally{ strs = null; }}//通过WMI获取COM端口/// /// 串口信息/// /// public static string[] GetSerialPort(){return MulGetHardwareInfo(HardwareEnum.Win32_PnPEntity, "Name");}}
}
调用方法
string[] ArryPort = GatSerialPortName.GetSerialPort();foreach (string txt in ArryPort)add(txt);
运行效果
测试环境vs2022,win10
特此记录
anlog
2022年12月3日