Digging into Network Adapters with Visual Basic.NET and VBScript using WMI - Listing network adapter information using WMI
(Page 2 of 4 )
How do you list all of the network adapters available on your system? We'll assume that you also want a list of all bridges and other logical network connections as well.
Before going to the WMI code, we need to define the cache to store that information. The following are the two routines I declared in "globals.vb" to hold the cache of information.
Public Function getNetworkAdapterStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("AdapterType"))
dt.Columns.Add(New DataColumn("MACAddress"))
dt.Columns.Add(New DataColumn("Manufacturer"))
Return dt
End Function
Public Sub addNetworkAdapterRow(ByRef dt As DataTable, ByVal
AdapterType As String, ByVal MACAddress As String, ByVal
Manufacturer As String)
Dim dr As DataRow
dr = dt.NewRow
dr("AdapterType") = AdapterType
dr("MACAddress") = MACAddress
dr("Manufacturer") = Manufacturer
dt.Rows.Add(dr)
End Sub
The following is the WMI code to retrieve our desired information.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Try
Dim searcher As New ManagementObjectSearcher( _
"rootCIMV2", _
"SELECT * FROM Win32_NetworkAdapter")
Dim dt As DataTable =
globals.getNetworkAdapterStructure
For Each queryObj As ManagementObject In searcher.Get
()
globals.addNetworkAdapterRow(dt, queryObj
("AdapterType"), queryObj("MACAddress"), queryObj
("Manufacturer"))
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
I excluded some of the properties (as it would make the program too long) from the existing WMI class (Win32_NetworkAdapter) to give you only the most important ones. The above program would list the MACAddress, Manufacturer and AdapterType. You can further extend the above program with several other properties available in the "Win32_NetworkAdapter" class. You can even refer to MSDN online for further properties.
You can achieve the same thing with VBScript using the following code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & strComputer &
"rootCIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapter",,48)
For Each objItem in colItems
Wscript.Echo "AdapterType: " & objItem.AdapterType
Wscript.Echo "MACAddress: " & objItem.MACAddress
Wscript.Echo "Manufacturer: " & objItem.Manufacturer
Next
Next: Listing extended information about network adapters using WMI >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee