Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • Administrators
Posted

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

 

       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 it's current form but it should get you started. From there you could also use nic("IPAddress") to get each IP address etc.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

 

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

 

       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 it's current form but it should get you started. From there you could also use nic("IPAddress") to get each IP address etc.

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...