The below function returns a list of serial port property lists with names and all available properties. I added an optional overload "ShowNullProperties" if set to TRUE will return all properties regardless if the value is null. The properties for "Caption" and "DeviceID" are manually added again at the end of the list so I can easily identify the port name and device ID when the list is returned without having to search the entire list. For the below code to work you will need a treeview called trv_ports in your designer and a image list however you can comment out the image list code and it will show a ? for the image icons.
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
Then in the Form1 Load event call this function and populate a treeview.
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
Output with ShowNullProperties = True: ( if set to False all values that show "Nothing" would not be added to the list)
