0

Microsoft 的文章ManagementBaseObject.PropertiesProperty 展示了如何枚举集合中的属性

    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 检索信息。

我想要做的是运行一个 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 不是一个集合。

4

1 回答 1

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
于 2012-12-17T22:00:01.873 回答