WMI Programming with Visual Basic.NET: Tips and Tricks - How does this program work?
(Page 5 of 5 )
The program can be divided into three parts, for our convenience. The first part deals with preparing the structure (columns of properties) of the data table. The second part fills the data table with all of the information of logical disks. Finally, the third part displays the information back to the form.
The following can be considered the first part:
DimdtAsNewDataTable
DimdiskAsNewManagementObject("Win32_LogicalDisk.deviceid='c:'")
DimdiskPropertiesAsPropertyDataCollection = disk.Properties
DimdiskPropertyAsPropertyData
ForEachdiskPropertyIndiskProperties
dt.Columns.Add(diskProperty.Name)
NextdiskProperty
The above fragment is almost similar to that of the first program we discussed. The main goal of the above fragment is to create columns of the data table. The columns should be the property names themselves. To retrieve the property names, we need to connect to at least one logical device (which would definitely be present). I am connecting to ‘c:’ in this case. Using the FOR loop, I am retrieving all property names and adding them as columns to the data table.
The following is the second part:
'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
If you carefully observe the above fragment, we are using nested loops. The outer loop works on each logical disk, and the inner loop works with the properties of the current logical disk of the outer loop. This fragment is almost similar to the examples I provided in part three, except that I am using an inner loop. The inner loop actually assigns the information about all of the properties (of a particular disk) to a data row, which gets added to the data table.
And I hope you can understand the following third part very easily.
'displaying
dt.AcceptChanges()
Me.DataGrid1.DataSource = dt
The above just ensures that data table is filled. It finally gets displayed using a data grid.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |