首页 > 解决方案 > SystemManagement - 查找设备实例路径

问题描述

附上我试图从C#代码转换为代码的代码。 我能够将标题添加到 ListView。VB.Net

我无法得到DeviceIDandCaption值。
代码中有一个循环。由于我将代码从C#代码VB.Net转换为转换器,因此可能存在不足。我在哪里犯了错误或此代码中缺少什么?我就是没能成功。

查找设备实例路径:

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

标签: vb.netwinformsc#-to-vb.netdevice-instance-idsystemmanagement

解决方案


推荐阅读