Find Subnet mask using VB.NET

linesh

Active member
Joined
Oct 14, 2004
Messages
32
Location
Washington D.C.
I tried to check in the different forums, but have not landed on the right strip yet on this. I am not sure if I WILL have to use Win API for this(thats how I did it in VB6). I have two questions:

1) Is there a way to find the subnet mask of the local machine programmatically using VB.NET?

2) Is there an easy way to find if a given IP Address is on the same subnet using VB.NET ?

Thanks in advance,
Linesh
 
Create a new project and add a reference to System.Management.dll, paste this code in a button click or similar.

Code:
        Dim mc As New Management.ManagementClass("Win32_NetworkAdapterConfiguration")

        Dim nics As Management.ManagementObjectCollection

        nics = mc.GetInstances()
        Dim nic As Management.ManagementObject

        For Each nic In nics
            If nic("ipEnabled") = True Then
                Dim s As String
                For Each s In nic("IPSubnet")
                    MessageBox.Show(s, nic("Caption"))
                Next
            End If
        Next

this should display the subnet mask for each address assigned to each nic, not very useful in its current form but it should get you started. From there you could also use nic("IPAddress") to get each IP address etc.
 
Thanks for the fast response!
I tried it and it works. Now how do I enumarate the IP Addresses. I get an error when I try to say nic("IPAddress")..I guess I have to extract the IP Address string from the string array.

PlausiblyDamp said:
Create a new project and add a reference to System.Management.dll, paste this code in a button click or similar.

Code:
        Dim mc As New Management.ManagementClass("Win32_NetworkAdapterConfiguration")

        Dim nics As Management.ManagementObjectCollection

        nics = mc.GetInstances()
        Dim nic As Management.ManagementObject

        For Each nic In nics
            If nic("ipEnabled") = True Then
                Dim s As String
                For Each s In nic("IPSubnet")
                    MessageBox.Show(s, nic("Caption"))
                Next
            End If
        Next

this should display the subnet mask for each address assigned to each nic, not very useful in its current form but it should get you started. From there you could also use nic("IPAddress") to get each IP address etc.
 
Back
Top