【问题标题】:Acces device information with C# or VB使用 C# 或 VB 访问设备信息
【发布时间】:2017-01-21 17:25:30
【问题描述】:

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

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

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

var win32DeviceClassName = "Win32_SerialPort";
var query = string.Format("select * from {0}", win32DeviceClassName);

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

附:我需要此信息,以找出哪个地址具有 wich com-port,然后将 comport 更改为所需的。

【问题讨论】:

标签: c# vb.net winapi


【解决方案1】:

试试这个:

    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

【讨论】:

  • 感谢您的回答,它给了我 vid/pid,但地址丢失了。我的输出如下所示:USB Serial Port (COM6) FTDIBUS\VID_0403+PID_6001+A901ET6UA\0000 但缺少像 00000001 这样的地址字段
【解决方案2】:

以下函数返回一个串行端口属性列表列表,其中包含名称和所有可用属性。我添加了一个可选的重载“ShowNullProperties”,如果设置为 TRUE,则无论值是否为空,都将返回所有属性。 “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”的值都不会添加到列表中)

【讨论】:

    猜你喜欢
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多