A downloadable file for this article is available here.
The sample downloadable solution (zip) is entirely developed using Visual Studio.NET 2003 Enterprise Architect on Windows Server 2003 Standard Edition. But, I am confident that it would work with other versions of Windows (which support .NET 1.1) as well.
I contributed several articles on WMI with VB.NET and VBScript previously(including a number that cover introductory or basic topics of WMI). I even contributed a series (of about six articles) on “WMI Programming on VB.NET” covering several aspects of WMI. I strongly suggest that you go through the series before going through this article.
How to list all IP addresses of a computer using Visual Basic.NET – preparing the wrapper
Before getting the information of “IPAddresses”, we need to create wrapper to store the “IPAddress” information. Let us proceed with creating a wrapper:
Public Function getNetworkInfoStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Caption"))
dt.Columns.Add(New DataColumn("Description"))
dt.Columns.Add(New DataColumn("DHCPEnabled"))
dt.Columns.Add(New DataColumn("DHCPServer"))
dt.Columns.Add(New DataColumn("DNSDomain"))
dt.Columns.Add(New DataColumn("DNSHostName"))
dt.Columns.Add(New DataColumn("IPAddress"))
Return dt
End Function
The following method “addNetworkInfo” adds a single row based on the structure you create for the data table using the above method.
Public Sub addNetworkInfo(ByRef dt As DataTable, ByVal Caption As String, ByVal Description As String, ByVal DHCPEnabled As String, ByVal DHCPServer As String, ByVal DNSDomain As String, ByVal DNSHostName As String, ByVal IPAddress As String)
Dim dr As DataRow
dr = dt.NewRow
dr("Caption") = Caption
dr("Description") = Description
dr("DHCPEnabled") = DHCPEnabled
dr("DHCPServer") = DHCPServer
dr("DNSDomain") = DNSDomain
dr("DNSHostName") = DNSHostName
dr("IPAddress") = IPAddress
dt.Rows.Add(dr)
End Sub
At the moment, I didn’t give the subnet for the IP Address to be listed. But you can have it through “IPSubmit”. You just need to add that as part of the structure to get it listed.
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()
In the previous section, I tried to retrieve only basic IP related information. But, if we need to go into the depth of the IP configuration, we still need to delve into a few more important properties. Before starting with the code, we need to have a separate wrapper for this concept again. The wrapper would look something like this:
Public Function getNetworkConfigStructure() As DataTable
The following method “addNetworkconfig” adds a single row based on the structure you create for the data table using the above method.
Public Sub addNetworkConfig(ByRef dt As DataTable, ByVal Caption As String, ByVal IPFilterSecurityEnabled As String, ByVal IPPortSecurityEnabled As String, ByVal IPXAddress As String, ByVal IPXEnabled As String, ByVal IPXNetworkNumber As String, ByVal MACAddress As String, ByVal WINSPrimaryServer As String, ByVal WINSSecondaryServer As String)
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 in-depth details of 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.getNetworkConfigStructure
For Each queryObj As ManagementObject In searcher.Get()
Before getting the information on “Network Clients” installed on your computer, we need to create a wrapper to store the same information. Let us proceed with creating a wrapper:
Public Function getNetworkClientStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Caption"))
dt.Columns.Add(New DataColumn("Description"))
dt.Columns.Add(New DataColumn("InstallDate"))
dt.Columns.Add(New DataColumn("Manufacturer"))
dt.Columns.Add(New DataColumn("Name"))
Return dt
End Function
The following method “addNetworkClient” adds a single row based on the structure you create for the data table using the above method.
Public Sub addNetworkClient(ByRef dt As DataTable, ByVal Caption As String, ByVal Description As String, ByVal InstallDate As String, ByVal Manufacturer As String, ByVal Name As String)
Dim dr As DataRow
dr = dt.NewRow
dr("Caption") = Caption
dr("Description") = Description
dr("InstallDate") = InstallDate
dr("Manufacturer") = Manufacturer
dr("Name") = Name
dt.Rows.Add(dr)
End Sub
Once you complete the creation of wrapper, the following VB.NET code should support it with some minimum information of “NetworkClients” 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_NetworkClient")
Dim dt As DataTable = globals.getNetworkClientStructure
For Each queryObj As ManagementObject In searcher.Get()
Before getting the information on “Win Proxy” installed/configured on your computer, we need to create a wrapper to store the same information. Let us proceed with creating a wrapper:
Public Function getProxyStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Description"))
dt.Columns.Add(New DataColumn("ProxyPortNumber"))
dt.Columns.Add(New DataColumn("ProxyServer"))
dt.Columns.Add(New DataColumn("ServerName"))
Return dt
End Function
The following method “addProxy” adds a single row based on the structure you create for the data table using the above method.
Public Sub addProxy(ByRef dt As DataTable, ByVal Description As String, ByVal ProxyPortNumber As String, ByVal ProxyServer As String, ByVal ServerName As String)
Dim dr As DataRow
dr = dt.NewRow
dr("Description") = Description
dr("ProxyPortNumber") = ProxyPortNumber
dr("ProxyServer") = ProxyServer
dr("ServerName") = ServerName
dt.Rows.Add(dr)
End Sub
Once you have created the wrapper, the following VB.NET code should support it with some minimum information of “WinProxies” 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_Proxy")
Dim dt As DataTable = globals.getNetworkClientStructure
For Each queryObj As ManagementObject In searcher.Get()