简体   繁体   English

在C#中检查COM端口可用性

[英]Checking COM Port Availability in C#

My C# application populates a comboBox with the COM ports found on the system. 我的C#应用​​程序使用系统上找到的COM端口填充comboBox I would like the mark the COM ports that are in use as such. 我想要标记正在使用的COM端口。 I know that I can use try / catch blocks to attempt to open every COM port, but I was wondering if there is a more graceful way to do this. 我知道我可以使用try / catch块尝试打开每个COM端口,但是我想知道是否有更优雅的方法可以做到这一点。 Perhaps using a WMI query? 也许使用WMI查询? I am using Microsoft Visual C# 2008 Express Edition (.NET 2.0). 我正在使用Microsoft Visual C#2008 Express Edition(.NET 2.0)。 Any thoughts or suggestions you may have would be appreciated. 您可能有任何想法或建议。 Thanks. 谢谢。

I'm not aware of any other method to do this. 我不知道有任何其他方法可以做到这一点。 Attempting to open them is the way that native code does the same check. 尝试打开它们是本机代码执行相同检查的方式。

I know this a Old. 我知道这很老。 But there is now a really easy way to do this. 但是,现在有一种非常简单的方法可以做到这一点。 You can check if a port is open with: 您可以使用以下方法检查端口是否打开:

if (serialPort1.IsOpen)
{
    ("Port Open");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM