Subnet Mask

how to get subnet mask using vb.net 2.0

Answer this question

Subnet Mask

  • Oscar Calvo

    I am looking for that too. You can however get that info from win32 wmi class lib.

    Win32_NetworkAdapterConfiguration and the IPSubnet is the property of that class.

    But how the heck do u do it in .net Everyone has given the example of how to get the IP Address using this code

    Dim IpEntry As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(Environment.MachineName)

    Dim IpAddr As System.Net.IPAddress() = IpEntry.AddressList

    Dim IpAddrIndex As Integer
    For IpAddrIndex = 0 To IpAddr.Length - 1

    Console.WriteLine(IpAddr(IpAddrIndex).ToString)

    Next

    the above code is from pcelement see this thread

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=223586&SiteID=1

    I was thinking that the guys who wrote that class tho forgot about all the other parts of the IpAddress. Subnet dns, gateway etc....

    Of course we want to do everything with .net now, no more funky win32 stuff right

    I am just trying to learn like the rest!!



  • DrBytes

    .net 2.0 doesnt have the facility of subnet mask

    the only way left is to use WMI

    i have already done it

    Here it is. Dont forget to add reference to system.mangement assembly.

    Sub GetSubnetMask()

    Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")

    Dim objMOC As ManagementObjectCollection = objMC.GetInstances()

    Dim objMO As ManagementObject

    Dim subnets As String()

    For Each objMO In objMOC

    subnets = CType(objMO("IPSubnet"), String())

    If subnets IsNot Nothing Then

    Console.WriteLine(String.Format("Subnet Mask: {0}", subnets(0)))

    End If

    Next

    End Sub


  • Chennai BOY

    Hmmmmm, not seeming to get an answer on this one, I wonder if this will be in .net 2.01 LOL

  • Subnet Mask