Finding Subnet to a specified local IP

CryoEnix

Regular
Joined
Jan 11, 2003
Messages
93
Location
Wrexham, Wales
Hi there, I've been playing around with networking in .NET lately, and I've 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 it's a LAN or Internet IP. Anyone?
 
PlausiblyDamp said:
clicky might be of some help

PlausiblyDamp, you're a star, a bloody star!

From that code you've given me I've 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