vb.net 如何获取使用 COM 端口的内容 Visual Basic (.NET)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17170126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:57:14  来源:igfitidea点击:

How to get what's using the COM Port Visual Basic (.NET)

vb.net

提问by Tim

I have this code here

我这里有这个代码

For i As Integer = 0 to My.Computer.Ports.SerialNames.Count - 1
      ComboBox1.Items.Add(My.Computer.Ports.SerialPortNames(i))
Next

Example Output:

示例输出:

COM1
COM2
COM3

this returns a list of used COM ports in a ComboBox

这将返回 ComboBox 中使用的 COM 端口列表

now what I want to do is something like this

现在我想做的是这样的

COM1 <USB Mouse>
COM2 <USB Keyboard>

etc.

等等。

I want to get whatever it is using the COM port.

我想得到任何使用 COM 端口的东西。

Hope anyone can help, thanks!

希望大家帮忙,谢谢!

回答by SysDragon

Here I found a link where your question is solved: Msdn forum

在这里,我找到了解决您问题的链接:Msdn 论坛

It uses Windows Management Instrumentation to retrieve the data you want from the ports. This way you will retrieve the full name of the port, including the part you want.

它使用 Windows Management Instrumentation 从端口检索您想要的数据。这样您将检索端口的全名,包括您想要的部分。

Here is the code:

这是代码:

' Add reference to System.Management.dll.
Try
    Dim searcher As New ManagementObjectSearcher( _
   "root\cimv2", _
   "SELECT * FROM Win32_SerialPort")

    For Each queryObj As ManagementObject In searcher.Get()
        MsgBox(queryObj("Name"))
    Next

Catch err As ManagementException
    MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try