微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SystemManagement - 查找设备实例路径

如何解决SystemManagement - 查找设备实例路径

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

我无法获取 deviceidCaption 值。
代码内部有一个循环。由于我使用转换器将代码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",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,CheckBox1.Checked)
    End Sub

End Class

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。