如何在 C# 中扫描 COM 端口? [英] How to scan for COM ports in C#?

查看:92
本文介绍了如何在 C# 中扫描 COM 端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C# 是否提供扫描可用 COM 端口的有效方法?我想在我的应用程序中有一个下拉列表,用户可以在其中选择检测到的 COM 端口之一.创建和填充下拉列表不是问题.我只需要知道如何使用 C# 扫描可用的 COM 端口.

Does C# provide an effective means of scanning the available COM ports? I would like to have a dropdown list in my application wherein the user can select one of the detected COM ports. Creating and populating the dropdown list is not a problem. I just need to know how to scan for the available COM ports using C#.

推荐答案

System.IO.Ports 是你想要的命名空间.

System.IO.Ports is the namespace you want.

SerialPort.GetPortNames列出所有串行 COM 端口.

SerialPort.GetPortNames will list all serial COM ports.

不幸的是,C# 不直接支持并行端口,因为它们很少使用,除非在传统情况下.也就是说,您可以通过查询以下注册表项来列出它们:

Unfortunately, parallel ports are not supported directly from C#, as they're very infrequently used except in legacy situations. That said, you can list them by querying the following registry key:

HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\PARALLEL PORTS

有关详细信息,请参阅 Microsoft.Win32 命名空间.

See the Microsoft.Win32 namespace for details.

这篇关于如何在 C# 中扫描 COM 端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆