使用C#或VB访问设备信息

使用C#或VB访问设备信息,c#,vb.net,winapi,C#,Vb.net,Winapi,如果我在Windows上打开设备管理器,然后转到“端口(COM LTP)”,我会看到7个设备。 1-内置计算机RS323 2-6-USB串行端口(COM X) 如果我右键单击->属性->详细信息,我可以看到一个大的值列表。 我感兴趣的是“地址”和“硬件ID”,即“FTDIBUS\COMPORT&VID_0403&PID_6001” 如何使用C#或更好的VB访问此信息? 我试过了 然后对每个属性进行控制台打印,但只有内置的COM1显示信息 另外,我需要这些信息,以找出哪个地址有哪个com端口,然后

如果我在Windows上打开设备管理器,然后转到“端口(COM LTP)”,我会看到7个设备。 1-内置计算机RS323 2-6-USB串行端口(COM X)

如果我右键单击->属性->详细信息,我可以看到一个大的值列表。 我感兴趣的是“地址”和“硬件ID”,即“FTDIBUS\COMPORT&VID_0403&PID_6001”

如何使用C#或更好的VB访问此信息? 我试过了

然后对每个属性进行控制台打印,但只有内置的COM1显示信息

另外,我需要这些信息,以找出哪个地址有哪个com端口,然后将端口更改为所需的端口

试试这个:

    Try
        Using mos As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE ClassGuid=""{4d36e978-e325-11ce-bfc1-08002be10318}""")
            Dim AvailableComPorts = SerialPort.GetPortNames().ToList()
            Dim q As ManagementObjectCollection = mos.Get()

            For Each x As ManagementObject In q
                Console.WriteLine(x.Properties("Name").Value)
                Console.WriteLine(x.Properties("DeviceID").Value)
            Next
        End Using
    Catch ex As Exception
        Throw
    End Try

下面的函数返回带有名称和所有可用属性的串行端口属性列表列表。我添加了一个可选重载“ShowNullProperties”,如果设置为TRUE,则无论该值是否为null,都将返回所有属性。“Caption”和“DeviceID”的属性会再次手动添加到列表的末尾,这样我就可以在返回列表时轻松识别端口名和设备ID,而不必搜索整个列表。要使下面的代码正常工作,您需要在设计器中使用一个名为trv_ports的树视图和一个图像列表,但是您可以注释掉图像列表代码,它将显示一个?对于图像图标

Imports System.Management

    Private Function Get_ListofSerialPorts(Optional ByVal ShowNullProperties As Boolean = False) As List(Of List(Of String))

    ' This function returns a list of serial port property lists 

    Dim RtnList As New List(Of List(Of String))

    Dim portSearcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM%'")

    For Each port As System.Management.ManagementObject In portSearcher.Get()
        Dim NewList As New List(Of String)

        For Each prop As PropertyData In port.Properties

            If ShowNullProperties = True Then
                ' Show null properties 
                If prop.Value IsNot Nothing Then NewList.Add(prop.Name.ToString & ": " & prop.Value.ToString) Else NewList.Add(prop.Name.ToString & ": " & "Nothing")
            Else
                ' Do not show null properties 
                If prop.Value IsNot Nothing Then NewList.Add(prop.Name.ToString & ": " & prop.Value.ToString)
            End If

        Next
        ' Add these two properties on the end to use later for the name and device ID fields 
        NewList.Add(port("Caption").ToString)
        NewList.Add(port("DeviceID").ToString)

        RtnList.Add(NewList)

    Next
    Return RtnList

End Function
然后在Form1 Load事件中调用此函数并填充树视图

      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    ' Initialized the Ports tree view and gets a list of availible ports 
    ' Load the image index and clear the treeview
    trv_Ports.ImageList = img_Icons ' Image list of icons 0 = serialport, 1 = Properties
    trv_Ports.Nodes.Clear() ' Clear out all nodes 

    ' Create the root node for the serial ports 
    Dim Newnode As New TreeNode
    Newnode.Text = "Ports (COM & LPT)" ' Parent Node
    Newnode.ImageIndex = 0
    Newnode.SelectedImageIndex = 0

    trv_Ports.Nodes.Add(Newnode)


    ' Step through each list and create a new node with the name of the caption and set the tag equal to the device id 
    For Each list In Get_ListofSerialPorts(True)
        Dim Newnode1 As New TreeNode
        ' Get the Device Name and Device ID from the last two items in the list then delete 
        Newnode1.Text = list(list.Count - 2) ' Device Name 
        Newnode1.Tag = list(list.Count - 1) ' Device ID
        list.Remove(Newnode1.Text) ' Now delete the last 2 entries which are the Name and ID 
        list.Remove(Newnode1.Tag)

        Newnode1.ImageIndex = 0 ' Show the serial port icon in the treeview
        Newnode1.SelectedImageIndex = 0

        trv_Ports.Nodes(0).Nodes.Add(Newnode1)

        Dim ListNode As New TreeNode
        For x As Integer = 0 To list.Count - 1

            ListNode = Newnode1.Nodes.Add(x)
            ListNode.Text = list(x)
            ListNode.Tag = Newnode1.Text & "," & list(x)
            ListNode.ImageIndex = 1 ' Show the properties icon
            ListNode.SelectedImageIndex = 1

        Next

    Next

    ' expand the availible ports node
    trv_Ports.Nodes(0).Expand()

    ' Collapse all the properties nodes
    For i As Integer = 0 To trv_Ports.Nodes(0).Nodes.Count - 1
        trv_Ports.Nodes(0).Nodes(i).Collapse()
    Next



End Sub
ShowNullProperties=True的输出:(如果设置为False,则不会将显示“Nothing”的所有值添加到列表中)


请参见此操作是否仅提供端口列表或所有属性?Win32_SerialPort没有显示可以手动查看的端口的“地址”。谢谢您的回答,它提供了vid/pid,但缺少地址。我的输出如下所示:
USB串行端口(COM6)
FTDIBUS\VID\u 0403+PID\u 6001+A901ET6UA\0000
但缺少类似
00000001
的地址字段
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    ' Initialized the Ports tree view and gets a list of availible ports 
    ' Load the image index and clear the treeview
    trv_Ports.ImageList = img_Icons ' Image list of icons 0 = serialport, 1 = Properties
    trv_Ports.Nodes.Clear() ' Clear out all nodes 

    ' Create the root node for the serial ports 
    Dim Newnode As New TreeNode
    Newnode.Text = "Ports (COM & LPT)" ' Parent Node
    Newnode.ImageIndex = 0
    Newnode.SelectedImageIndex = 0

    trv_Ports.Nodes.Add(Newnode)


    ' Step through each list and create a new node with the name of the caption and set the tag equal to the device id 
    For Each list In Get_ListofSerialPorts(True)
        Dim Newnode1 As New TreeNode
        ' Get the Device Name and Device ID from the last two items in the list then delete 
        Newnode1.Text = list(list.Count - 2) ' Device Name 
        Newnode1.Tag = list(list.Count - 1) ' Device ID
        list.Remove(Newnode1.Text) ' Now delete the last 2 entries which are the Name and ID 
        list.Remove(Newnode1.Tag)

        Newnode1.ImageIndex = 0 ' Show the serial port icon in the treeview
        Newnode1.SelectedImageIndex = 0

        trv_Ports.Nodes(0).Nodes.Add(Newnode1)

        Dim ListNode As New TreeNode
        For x As Integer = 0 To list.Count - 1

            ListNode = Newnode1.Nodes.Add(x)
            ListNode.Text = list(x)
            ListNode.Tag = Newnode1.Text & "," & list(x)
            ListNode.ImageIndex = 1 ' Show the properties icon
            ListNode.SelectedImageIndex = 1

        Next

    Next

    ' expand the availible ports node
    trv_Ports.Nodes(0).Expand()

    ' Collapse all the properties nodes
    For i As Integer = 0 To trv_Ports.Nodes(0).Nodes.Count - 1
        trv_Ports.Nodes(0).Nodes(i).Collapse()
    Next



End Sub