C# .net中获取台式电脑中串口设备的名称

1、情境:

做项目的时候要打开串口然后进行一些库函数的调用来操作目标板。串口使用的是usb转串口,板子插进拔出的,每次都使用不一样的usb口,debug的时候懒得每次改com口,又不想在UI上加上一个选择

com口的combox,于是就使用了下面这个方法。

2、环境:

win7 64、vs2010

3、目标:

获取下图的设备到底使用的是com几。

C# .net中获取台式电脑中串口设备的名称_第1张图片

4、source code

1 ///


2 /// Get the target com num.
3 ///

4 ///
5 public static int GetComNum()
6 {
7 int comNum = -1;
8 string[] strArr = GetHarewareInfo(HardwareEnum.Win32_PnPEntity, "Name");
9 foreach (string s in strArr)
10 {
11 Debug.WriteLine(s);
12
13 if (s.Length >= 23 && s.Contains("CH340"))
14 {
15 int start = s.IndexOf("(") + 3;
16 int end = s.IndexOf(")");
17 comNum = Convert.ToInt32(s.Substring(start + 1, end - start - 1));
18 }
19 }
20
21 return comNum;
22
23 }
24
25 ///
26 /// Get the system devices information with windows api.
27 ///

28 /// Device type.
29 /// the property of the device.
30 ///
31 private static string[] GetHarewareInfo(HardwareEnum hardType, string propKey)
32 {
33
34 List strs = new List();
35 try
36 {
37 using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + hardType))
38 {
39 var hardInfos = searcher.Get();
40 foreach (var hardInfo in hardInfos)
41 {
42 if (hardInfo.Properties[propKey].Value != null)
43 {
44 String str = hardInfo.Properties[propKey].Value.ToString();
45 strs.Add(str);
46 }
47
48 }
49 }
50 return strs.ToArray();
51 }
52 catch
53 {
54 return null;
55 }
56 finally
57 {
58 strs = null;
59 }
60 }//end of func GetHarewareInfo().
61
62 ///
63 /// 枚举win32 api
64 ///

65 public enum HardwareEnum
66 {
67 // 硬件
68 Win32_Processor, // CPU 处理器
69 Win32_PhysicalMemory, // 物理内存条
70 Win32_Keyboard, // 键盘
71 Win32_PointingDevice, // 点输入设备,包括鼠标。
72 Win32_FloppyDrive, // 软盘驱动器
73 Win32_DiskDrive, // 硬盘驱动器
74 Win32_CDROMDrive, // 光盘驱动器
75 Win32_BaseBoard, // 主板
76 Win32_BIOS, // BIOS 芯片
77 Win32_ParallelPort, // 并口
78 Win32_SerialPort, // 串口
79 Win32_SerialPortConfiguration, // 串口配置
80 Win32_SoundDevice, // 多媒体设置,一般指声卡。
81 Win32_SystemSlot, // 主板插槽 (ISA & PCI & AGP)
82 Win32_USBController, // USB 控制器
83 Win32_NetworkAdapter, // 网络适配器
84 Win32_NetworkAdapterConfiguration, // 网络适配器设置
85 Win32_Printer, // 打印机
86 Win32_PrinterConfiguration, // 打印机设置
87 Win32_PrintJob, // 打印机任务
88 Win32_TCPIPPrinterPort, // 打印机端口
89 Win32_POTSModem, // MODEM
90 Win32_POTSModemToSerialPort, // MODEM 端口
91 Win32_DesktopMonitor, // 显示器
92 Win32_DisplayConfiguration, // 显卡
93 Win32_DisplayControllerConfiguration, // 显卡设置
94 Win32_VideoController, // 显卡细节。
95 Win32_VideoSettings, // 显卡支持的显示模式。
96
97 // 操作系统
98 Win32_TimeZone, // 时区
99 Win32_SystemDriver, // 驱动程序
100 Win32_DiskPartition, // 磁盘分区
101 Win32_LogicalDisk, // 逻辑磁盘
102 Win32_LogicalDiskToPartition, // 逻辑磁盘所在分区及始末位置。
103 Win32_LogicalMemoryConfiguration, // 逻辑内存配置
104 Win32_PageFile, // 系统页文件信息
105 Win32_PageFileSetting, // 页文件设置
106 Win32_BootConfiguration, // 系统启动配置
107 Win32_ComputerSystem, // 计算机信息简要
108 Win32_OperatingSystem, // 操作系统信息
109 Win32_StartupCommand, // 系统自动启动程序
110 Win32_Service, // 系统安装的服务
111 Win32_Group, // 系统管理组
112 Win32_GroupUser, // 系统组帐号
113 Win32_UserAccount, // 用户帐号
114 Win32_Process, // 系统进程
115 Win32_Thread, // 系统线程
116 Win32_Share, // 共享
117 Win32_NetworkClient, // 已安装的网络客户端
118 Win32_NetworkProtocol, // 已安装的网络协议
119 Win32_PnPEntity,//all device
120 }

以上代码是在WinForm程序中实现的,如果要在WEB网页中实现获取客户端电脑的串口信息,要用到以下代码:
首先要引用System.IO
然后用数组存放客户端电脑现有串口列表,然后将数组项关联到页面控件上,如DropDownList下拉列表,以供用户选择。

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]);
}
}
================================================================
问:
需要在浏览器环境下获取用户cpu、显卡、主板、硬盘和内存信息,请问用什么方法获取?如果不可行的话,为什么?

答:
必须使用activex外挂软件的方法才能获取,不信你可以去看,有显示卡网站提供自动检查硬件下载驱动的功能,是不是提示你安装一个控件才能检测。
之所以不可以,是浏览器本身功能的局限和限制。

为什么功能局限,因为功能越少,越容易被设计为跨平台。如果你要获取cpu 显卡主板 硬盘,必然要和操作系统打交道,和硬件打交道,需要特定的本地程序,那么手机、机顶盒、电子书阅读器怎么访问你的网站。

另一个就是安全性的考虑,浏览器不允许网站直接访问客户端的硬件,甚至关键文件和执行程序,否则恶意网站可以随意操作你的电脑,多恐怖。你想想你每天上多少不同的网站,但是你电脑才安装了几个软件。这两者风险完全不在一个数量级

因为网站本身的权限就有一定限制,出于安全考虑,WEB网页方式无法直接获取客户端其它的硬件信息,如CPUID,内存芯片,硬盘序列号等等信息,但是常见的比如CPU,内存,硬盘的基本信息,这些是可以探测到的,但是对于硬件的型号,参数,这类与客户端硬件信息相关操作的功能,一般还是使用WinForm应用程序实现。
确实要在WEB网页中实现的,需要通过ActiveX控件(仅支持IE浏览器),或者flash里的as脚本来获取。

参见:
使用VS2010 C#开发ActiveX控件(上)
https://www.cnblogs.com/yungboy/archive/2011/01/10/1932433.html
使用VS2010 C#开发ActiveX控件(下),完整代码下载
https://www.cnblogs.com/yungboy/archive/2011/01/11/1932438.html

转自:https://www.cnblogs.com/hshuzhao/p/4028856.html