سؤال

Microsoft's article, ManagementBaseObject.Properties Property, shows how to enumerate the properties in a collection

    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 

And This article http://msdn.microsoft.com/en-us/library/ms257359.aspx demonstrates retreiving info from WMI.

What I am trying to do is run a WMI query like "SELECT * FROM Win32_Environment". But I can't seem to enumerate through the collection because envVar is not a 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

How do I enumerate through this collection without knowing the names? I can't seem to get it to work since queryObj is not a collection.

هل كانت مفيدة؟

المحلول

If my memory serves me well, your loop should be something like this

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top