WMI Programming with Visual Basic.NET: Tips and Tricks - How does the program work?
(Page 3 of 5 )
Let me explain the above program line by line.
DimdtAsNewDataTable
dt.Columns.Add("Name")
dt.Columns.Add("Value")
The above set of statements prepare a data table (for storing all properties with their values) with only two columns, namely “Name” and “Value”.
DimdiskAsNewManagementObject("Win32_LogicalDisk.deviceid='c:'")
The above statement retrieves only a single instance of the “Win32_LogicalDisk” class based on the filter ‘deviceID=c:’. This excludes the use of unnecessary objects of type “ManagementObjectCollection”, as we are working with only a single instance.
DimdiskPropertiesAsPropertyDataCollection = disk.Properties
The above statement retrieves all properties along with all values using “disk.Properties” and stores them in the “diskProperties” object.
DimdiskPropertyAsPropertyData
The above statement declares “diskProperty” to access each and every property from “diskProperties” collection.
ForEachdiskPropertyIndiskProperties
DimdrAsDataRow = dt.NewRow
dr("Name") = diskProperty.Name
dr("Value") = diskProperty.Value
dt.Rows.Add(dr)
NextdiskProperty
The above loop iterates through each and every property existing in the “diskProperties” collection using “diskProperty” and assigns property information to a new individual data row, which finally gets added to the data table.
dt.AcceptChanges()
The above statement ensures that the data table is filled with all of the contents from memory.
Me.DataGrid1.DataSource = dt
The above statement displays all of the information available in the data table through a data grid.
Next: How can we extend the above program to work with all objects? >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee