[英]Acces device information with C# or VB
如果我在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显示信息
PS我需要此信息,以找出哪个地址具有最常用的端口,然后将其更改为所需的端口。
尝试这个:
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都将返回所有属性。 在列表的末尾再次手动添加了“标题”和“设备ID”的属性,这样我在返回列表时可以轻松地标识端口名和设备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”的所有值都不会添加到列表中)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.