The code I tried to convert from a C#
code to VB.Net
code is attached.
I was able to add the headers to a ListView.
I couldn't get the DeviceID
and Caption
values.
There is a loop inside the code. Since I converted the code from C#
code to VB.Net
with a converter, there might be deficiencies. Where am I making a mistake or what is missing in this code? I just couldn't succeed.
Find device instance path:
Imports System
Imports System.Collections
Imports System.Management
Imports System.Collections.Generic
Imports System.Windows.Forms
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.SelectedItem = "Win32_PnPEntity"
End Sub
#Disable Warning CA1822
Private Sub InsertInfo(ByVal Key As String, ByRef lst As ListView, ByVal DontInsertNull As Boolean)
#Enable Warning CA1822
lst.Items.Clear()
Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher(("select DeviceID, Caption from " + Key))
Try
For Each share As ManagementObject In searcher.Get
Dim grp As ListViewGroup
Try
grp = lst.Groups.Add(share("Name").ToString, share("Name").ToString)
Catch 'As System.Exception
grp = lst.Groups.Add(share.ToString, share.ToString)
End Try
If share.Properties.Count <= 0 Then
MessageBox.Show("No Information Available", "No Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
End If
For Each PC As PropertyData In share.Properties
Dim item As ListViewItem = New ListViewItem(grp)
If (lst.Items.Count Mod 2) <> 0 Then
item.BackColor = Color.White
Else
item.BackColor = Color.WhiteSmoke
End If
item.Text = PC.Name
If PC.Value IsNot Nothing AndAlso PC.Value.ToString <> "" Then
Select Case PC.Value.GetType.ToString
Case "System.String[]"
Dim str() As String = CType(PC.Value, String())
Dim str2 As String = ""
Dim st As String
For Each st In str
str2 += (st + " ")
Next
item.SubItems.Add(str2)
Case "System.UInt16[]"
Dim shortData() As System.UInt16 = CType(PC.Value, System.UInt16())
Dim tstr2 As String = ""
For Each st As System.UInt16 In shortData
tstr2 += (st.ToString + " ")
Next
item.SubItems.Add(tstr2)
Case Else
item.SubItems.Add(PC.Value.ToString)
End Select
ElseIf Not DontInsertNull Then
item.SubItems.Add("No Information available")
Else
'TODO: Warning!!! continue Else
End If
lst.Items.Add(item)
Next
Next
Catch exp As Exception
MessageBox.Show(("can't get data because of the followeing error " & vbLf + exp.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
Private Shared Sub RemoveNullValue(ByRef ListView1 As ListView)
For Each item As ListViewItem In ListView1.Items
If (item.SubItems(1).Text = "No Information available") Then
item.Remove()
End If
Next
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
RemoveNullValue(ListView1)
Else
InsertInfo(ComboBox1.SelectedItem.ToString, ListView1, CheckBox1.Checked)
End If
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
InsertInfo(ComboBox1.SelectedItem.ToString, ListView1, CheckBox1.Checked)
End Sub
End Class