Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Chaos is merely logic beyond human comprehension
Posted
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:

 

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

Chaos is merely logic beyond human comprehension

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...