WMI Programming with Visual Basic.NET: Tips and Tricks - How can we extend the above program to work with all objects?
(Page 4 of 5 )
The above example demonstrates working with only one instance. How can I work with more than one instance simultaneously? In other words, I would like to have each of the device details presented as a single row of information (columns being all the properties and not the rows, as demonstrated previously).
It is not a different concept, from the WMI point of view. It is just our logic as to how it is implemented. In the previous example, we added property information as a row. Now all the property names should be the columns of the data table. We need to retrieve the instances of logical devices and present them in different rows of the same data table (separated with property values as column values). The following program example gives an idea of the approach.
'preparing data table structure
DimdtAsNewDataTable
DimdiskAsNewManagementObject("Win32_LogicalDisk.deviceid='c:'")
DimdiskPropertiesAsPropertyDataCollection = disk.Properties
DimdiskPropertyAsPropertyData
ForEachdiskPropertyIndiskProperties
dt.Columns.Add(diskProperty.Name)
NextdiskProperty
'populating with rows
DimsearcherAsNewManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk")
DimdisksAsManagementObjectCollection = searcher.Get()
ForEachdiskIndisks
DimdrAsDataRow = dt.NewRow
diskProperties = disk.Properties
ForEachdiskPropertyIndiskProperties
dr(diskProperty.Name) = diskProperty.Value
NextdiskProperty
dt.Rows.Add(dr)
Next
'displaying
dt.AcceptChanges()
Me.DataGrid1.DataSource = dt
Even though, the above program looks bit lengthy, it is very simple to understand. The next section is dedicated to a detailed discussion of the above program.
Next: How does this program work? >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee