如何检查COM端口是否被C#中的另一个进程打开?

How to check if COM port is open by another process in C#?

我有以下问题。我必须检查我选择使用的 com 端口是否未被系统中的另一个进程使用。我尝试通过检查 System.IO.Ports.SerialPort 中的字段对象字段 IsOpen 来实现这一点,但即使端口在另一个进程中打开,结果也是 False 所以我的程序尝试打开端口并崩溃应有的例外。除了在 try catch 中处理这个问题还有其他方法吗?

您可以拨打打开的留言,寻找UnauthorizedAccessException来判断是否免费。

Access is denied to the port. - or - The current process, or another process on the system, already has the specified COM port open either by a SerialPort instance or in unmanaged code.

如果您在已经打开端口的实例上打开端口,您将得到 InvalidOperationException 异常。