linesh Posted December 2, 2004 Posted December 2, 2004 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 Quote
Administrators PlausiblyDamp Posted December 2, 2004 Administrators Posted December 2, 2004 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
linesh Posted December 2, 2004 Author Posted December 2, 2004 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. Quote
linesh Posted December 2, 2004 Author Posted December 2, 2004 Ok, here is a clearer question. How do I know if a given IP Address is on my subnet or not. Thanks, Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.