Retrieving Networking Configuration Information Using Visual Basic.NET and VBScript - How to List all IPAddresses of a computer using Visual Basic.NET – the code
(Page 2 of 6 )
Once you complete the creation of the wrapper (as specified in the previous section), proceed with the following VB.NET code to list all the details of the network configuration information 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_NetworkAdapterConfiguration WHERE IPEnabled = True")
Dim dt As DataTable = globals.getNetworkInfoStructure
For Each queryObj As ManagementObject In searcher.Get()
Dim IPAddress As String = ""
If queryObj("IPAddress") Is Nothing Then
IPAddress = queryObj("IPAddress")
Else
Dim arrIPAddress As String()
arrIPAddress = queryObj("IPAddress")
For Each arrValue As String In arrIPAddress
IPAddress &= arrValue
Next
End If
globals.addNetworkInfo(dt, queryObj("Caption"), queryObj("Description"), queryObj("DHCPEnabled"), queryObj("DHCPServer"), queryObj("DNSDomain"), queryObj("DNSHostName"), IPAddress)
Next
Me.DataGrid1.DataSource = dt
Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try
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_NetworkAdapterConfiguration WHERE IPEnabled = True",,48)
For Each objItem in colItems
Wscript.Echo "Caption: " & objItem.Caption
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "DHCPEnabled: " & objItem.DHCPEnabled
Wscript.Echo "DHCPServer: " & objItem.DHCPServer
Wscript.Echo "DNSDomain: " & objItem.DNSDomain
Wscript.Echo "DNSHostName: " & objItem.DNSHostName
If isNull(objItem.IPAddress) Then
Wscript.Echo "IPAddress: "
Else
Wscript.Echo "IPAddress: " & Join(objItem.IPAddress, ",")
End If
Next
Next: How to retrieve MAC address and other miscellaneous network information using Visual Basic.NET – preparing the wrapper >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee