枚举vb.net中的Properties集合

时间:2012-12-17 21:54:44

标签: vb.net wmi wmi-query

Microsoft的文章ManagementBaseObject.Properties Property显示了如何枚举集合中的属性

    For Each p As PropertyData In properties
        Console.WriteLine(p.Name)
        For Each q As QualifierData In p.Qualifiers
            If (q.Name.Equals("Description")) Then
                Console.WriteLine(processClass.GetPropertyQualifierValue(.Name, q.Name))
            End If 
        Next
        Console.WriteLine()
    Next 
End Function 

本文http://msdn.microsoft.com/en-us/library/ms257359.aspx演示了WMI的retreiving信息。

我要做的是运行WMI查询,例如“SELECT * FROM Win32_Environment”。但我似乎无法枚举整个集合,因为envVar不是PropertyDataCollection:

Dim query As New ObjectQuery(strQuery)
Dim searcher As New ManagementObjectSearcher(scope, query)
For Each queryObj As ManagementObject In searcher.Get()
   s = s & queryObj("Name").ToString() & ": " & queryObj("VariableValue").ToString()
Next

如何在不知道名字的情况下枚举此系列?我似乎无法让它工作,因为queryObj不是一个集合。

1 个答案:

答案 0 :(得分:0)

如果我的记忆很好,那么你的循环应该是这样的

Dim searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Environment")
Dim queryObj As ManagementObjectCollection = searcher.Get()
For Each mo in queryObj
    Console.WriteLine("----------------")
    For Each prop in mo.Properties
        Console.WriteLine("{0}: {1}", prop.Name, prop.Value)
    Next
Next