Retrieving Networking Configuration Information Using Visual Basic.NET and VBScript

This article explains how to retrieve “Network settings” information dynamically using Visual Basic.NET and VBScript.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 13
January 11, 2006
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

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. 

How to List all IPAddresses of a computer using Visual Basic.NET – the code

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

How to retrieve MAC address and other miscellaneous network information using Visual Basic.NET – preparing the wrapper

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
        Dim dt As New DataTable
        dt.Columns.Add(New DataColumn("Caption"))
        dt.Columns.Add(New DataColumn("IPFilterSecurityEnabled"))
        dt.Columns.Add(New DataColumn("IPPortSecurityEnabled"))
        dt.Columns.Add(New DataColumn("IPXAddress"))
        dt.Columns.Add(New DataColumn("IPXEnabled"))
        dt.Columns.Add(New DataColumn("IPXNetworkNumber"))
        dt.Columns.Add(New DataColumn("MACAddress"))
        dt.Columns.Add(New DataColumn("WINSPrimaryServer"))
        dt.Columns.Add(New DataColumn("WINSSecondaryServer"))
        Return dt
    End Function

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)
        Dim dr As DataRow
        dr = dt.NewRow
        dr("Caption") = Caption
        dr("IPFilterSecurityEnabled") = IPFilterSecurityEnabled
        dr("IPPortSecurityEnabled") = IPPortSecurityEnabled
        dr("IPXAddress") = IPXAddress
        dr("IPXEnabled") = IPXEnabled
        dr("IPXNetworkNumber") = IPXNetworkNumber
        dr("MACAddress") = MACAddress
        dr("WINSPrimaryServer") = WINSPrimaryServer
        dr("WINSSecondaryServer") = WINSSecondaryServer
        dt.Rows.Add(dr)
    End Sub

How to retrieve MAC address and other miscellaneous network information using Visual Basic.NET – the code

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()
                Dim IPXNetworkNumber As String = ""
                If queryObj("IPXNetworkNumber") Is Nothing Then
                    IPXNetworkNumber = queryObj("IPXNetworkNumber")
                Else
                    Dim arrIPXNetworkNumber As String()
                    arrIPXNetworkNumber = queryObj("IPXNetworkNumber")
                    For Each arrValue As String In arrIPXNetworkNumber
                        IPXNetworkNumber &= arrValue
                    Next
                End If
                globals.addNetworkConfig(dt, queryObj("Caption"), queryObj("IPFilterSecurityEnabled"), queryObj("IPPortSecurityEnabled"), queryObj("IPXAddress"), queryObj("IPXEnabled"), IPXNetworkNumber, queryObj("MACAddress"), queryObj("WINSPrimaryServer"), queryObj("WINSSecondaryServer"))
            Next
            Me.DataGrid1.DataSource = dt
        Catch err As Exception
            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 "IPFilterSecurityEnabled: " & objItem.IPFilterSecurityEnabled
    Wscript.Echo "IPPortSecurityEnabled: " & objItem.IPPortSecurityEnabled
    Wscript.Echo "IPXAddress: " & objItem.IPXAddress
    Wscript.Echo "IPXEnabled: " & objItem.IPXEnabled
    If isNull(objItem.IPXNetworkNumber) Then
        Wscript.Echo "IPXNetworkNumber: "
    Else
        Wscript.Echo "IPXNetworkNumber: " & Join(objItem.IPXNetworkNumber, ",")
    End If
    Wscript.Echo "MACAddress: " & objItem.MACAddress
    Wscript.Echo "WINSPrimaryServer: " & objItem.WINSPrimaryServer
    Wscript.Echo "WINSSecondaryServer: " & objItem.WINSSecondaryServer
Next

How to retrieve Network Client Information using Visual Basic.NET

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()
globals.addNetworkClient(dt, queryObj("Caption"), queryObj("Description"), queryObj("InstallDate"), queryObj("Manufacturer"), queryObj("Name"))
            Next
            Me.DataGrid1.DataSource = dt
        Catch err As Exception
            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_NetworkClient",,48)
For Each objItem in colItems
    Wscript.Echo "Caption: " & objItem.Caption
    Wscript.Echo "Description: " & objItem.Description
    Wscript.Echo "InstallDate: " & objItem.InstallDate
    Wscript.Echo "Manufacturer: " & objItem.Manufacturer
    Wscript.Echo "Name: " & objItem.Name
Next

How to retrieve Win Proxy Information using Visual Basic.NET

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()
                Console.WriteLine("Description: {0}", queryObj("Description"))
                Console.WriteLine("ProxyPortNumber: {0}", queryObj("ProxyPortNumber"))
                Console.WriteLine("ProxyServer: {0}", queryObj("ProxyServer"))
                Console.WriteLine("ServerName: {0}", queryObj("ServerName"))
            Next
            Me.DataGrid1.DataSource = dt
        Catch err As Exception
            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_Proxy",,48)
For Each objItem in colItems
    Wscript.Echo "Description: " & objItem.Description
    Wscript.Echo "ProxyPortNumber: " & objItem.ProxyPortNumber
    Wscript.Echo "ProxyServer: " & objItem.ProxyServer
    Wscript.Echo "ServerName: " & objItem.ServerName
Next

Any comments, suggestions, bugs, errors, feedback etc. are highly appreciated at jag_chat@yahoo.com.

blog comments powered by Disqus
VISUAL BASIC.NET ARTICLES

- Basic Form Properties and Modality in VB.NET
- Multiple Document Interfaces in Visual Basic
- Visual Basic for Beginners
- ASP.NET Image to PDF with VB.Net
- MySQL in ASP.NET: Mono using VB.NET
- AsyncFileUpload File Type and File Size Vali...
- Visual Studio: Adding Functionality and Style
- Clocks and Countdowns
- User-defined Functions using Visual Basic Ap...
- Understanding Object Binding in VBA
- Mastering the Message Box
- Testing a Windows Forms Application
- Using Visual Basic.NET Features to Code a Wi...
- Correcting Code in a Windows Forms Applicati...
- Write Readable Code and Comments for Windows...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials