Getting Hardware Information using Visual Basic.NET and VBScript continued - Listing the “BUS” information
(Page 2 of 5 )
Some scenarios may require some properties of the BUS to be listed. We can retrieve the same information dynamically using VB.NET. Before trying to retrieve the hardware information, let us create the wrapper with BUS structure:
Public Function getBusStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("BusType"))
dt.Columns.Add(New DataColumn("DeviceID"))
dt.Columns.Add(New DataColumn("PNPDeviceID"))
dt.Columns.Add(New DataColumn("SystemName"))
Return dt
End Function
The following method “addBus” adds a single row based on the structure you create for the data table using the above method.
Public Sub addBus(ByRef dt As DataTable, ByVal BusType As String, ByVal DeviceID As String, ByVal PNPDeviceID As String, ByVal SystemName As String)
Dim dr As DataRow
dr = dt.NewRow
dr("BusType") = BusType
dr("DeviceID") = DeviceID
dr("PNPDeviceID") = PNPDeviceID
dr("SystemName") = SystemName
dt.Rows.Add(dr)
End Sub
Once you complete the creation of wrapper, the following VB.NET code should support retrieving some minimum information about the BUS available on your system.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", _
"SELECT * FROM Win32_Bus")
Dim dt As DataTable = globals.getBusStructure
For Each queryObj As ManagementObject In searcher.Get()
globals.addBus(dt, Convert.ToString(queryObj("BusType")), queryObj("DeviceID"), queryObj("PNPDeviceID"), queryObj("SystemName"))
Next
Me.DataGrid1.DataSource = dt
Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try
End Sub
You can achieve the same thing with VBScript as follows:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_Bus",,48)
For Each objItem in colItems
Wscript.Echo "BusType: " & objItem.BusType
Wscript.Echo "DeviceID: " & objItem.DeviceID
Wscript.Echo "PNPDeviceID: " & objItem.PNPDeviceID
Wscript.Echo "SystemName: " & objItem.SystemName
Next
Next: Listing the “MotherBoard” information >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee