Finding Subnet to a specified local IP

CryoEnix

Well-known member
Joined
Jan 11, 2003
Messages
93
Location
Wrexham, Wales
Hi there, Ive been playing around with networking in .NET lately, and Ive managed to develop a function to return all of the IP addresses of the local machine - yay! But I digress...
The problem is, I need to find the subnet mask for each of these IP addresses - for example, if I were to pass the IP "192.168.0.1" to a function, I would like it to return the subnet (in this case "255.255.255.0"), or even just a string/boolean telling me if its a LAN or Internet IP. Anyone?
 
PlausiblyDamp said:
clicky might be of some help

PlausiblyDamp, youre a star, a bloody star!

From that code youve given me Ive been able to make this function, I hope it helps other people like it helped me:

Code:
Public Function GetMaskFromIP(ByVal TheIP As String) As String


        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("IPaddress")
                    If s = TheIP Then Exit For
                Next

                If s <> TheIP Then GoTo TryNextIP

                s = ""

                For Each s In nic("IPSubnet")
                    GetMaskFromIP = s
                Next


            End If
TryNextIP:
        Next

    End Function
 
Back
Top