Results 1 to 8 of 8

Thread: [02/03] How to get Local IP Address, Subnet and Gateway?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    16

    Unhappy [02/03] How to get Local IP Address, Subnet and Gateway?

    pls someone help me.. How to get Local IP Address, Subnet and Gateway? thanks...

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [02/03] How to get Local IP Address, Subnet and Gateway?

    For the LocalIP, try
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.  
    3. Dim LocalIP As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)
    4.  
    5. MessageBox.Show(LocalIP.AddressList.GetValue(0).ToString)
    6. End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    16

    Re: [02/03] How to get Local IP Address, Subnet and Gateway?

    i also need Subnet and Gateway.. thanks..

  4. #4
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: [02/03] How to get Local IP Address, Subnet and Gateway?

    i dont know if there is an easy way

    vb Code:
    1. Registry.LocalMachine\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\

    check for DhcpDefaultGateway,DhcpIPAddress,DhcpSubnetMask

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: [02/03] How to get Local IP Address, Subnet and Gateway?

    How I do it:
    Code:
    Public Class Form1
       Dim myInterfaces As New AllInterfacesClass
       Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
           myInterfaces = RefreshInterfaces("192.168.1.1")
           Debug.WriteLine(myInterfaces.OS)
           For Each intrfc In myInterfaces.interfaceList
               Debug.WriteLine("Interface -> " & intrfc.Description)
               Debug.WriteLine(intrfc.InterfaceType)
               Debug.WriteLine(intrfc.SpeedInMBPS.ToString)
               Debug.WriteLine(intrfc.Status)
           Next
       End Sub
    End Class
    Module InterfaceInfo
       'gathers some system info and network interface info
       Public Function RefreshInterfaces(Optional ByVal GatewayIP As String = "") _
                       As AllInterfacesClass
           Dim myInterfaces As AllInterfacesClass = GetInterfaceInfo()
           If Not (GatewayIP = "" OrElse myInterfaces Is Nothing _
                   OrElse myInterfaces.interfaceList Is Nothing) Then
               'determine IP Address associated with Gateway
               myInterfaces.IP_FacingInternet = ""
               'determine which of our addresses was used to access the internet
               For Each intf In myInterfaces.interfaceList
                   If Not intf.isLoop Then
                       For Each gw In intf.GWAddr 'look for GW match
                           If gw.Address = GatewayIP Then 'does it match?
                               'yes
                               'convert address to number
                               Dim gwIP As Net.IPAddress = Net.IPAddress.Parse(gw.Address)
                               Dim gwIP_L As Long = IPtoNum(gwIP)
                               'look at each ip address
                               For Each ip In intf.IPAddr
                                   'convert IP address to number
                                   Dim nIP As Net.IPAddress = Net.IPAddress.Parse(ip.Address)
                                   Dim nIP_L As Long = IPtoNum(nIP)
                                   'convert mask to number
                                   Dim mIP As Net.IPAddress = Net.IPAddress.Parse(ip.Mask)
                                   Dim mIP_L As Long = IPtoNum(mIP)
                                   'apply mask
                                   Dim gwM As Long = gwIP_L And mIP_L
                                   Dim ipM As Long = nIP_L And mIP_L
                                   If gwM = ipM Then myInterfaces.IP_FacingInternet = ip.Address
                               Next
                               If myInterfaces.IP_FacingInternet <> "" Then Exit For
                           End If
                       Next
                   End If
                   If myInterfaces.IP_FacingInternet <> "" Then Exit For
               Next
           End If
           Return myInterfaces
       End Function
       Private Function GetInterfaceInfo() As AllInterfacesClass
           Dim ipProperties As Net.NetworkInformation.IPGlobalProperties = _
                   Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties
           Dim myInterfaces As New AllInterfacesClass
           'get some system information
           myInterfaces.MemoryTotalMB = My.Computer.Info.TotalPhysicalMemory \ CULng((1024 * 1024))
           myInterfaces.MemoryAvailableMB = My.Computer.Info.AvailablePhysicalMemory \ CULng((1024 * 1024))
           myInterfaces.OS = My.Computer.Info.OSFullName & "  " & My.Computer.Info.OSVersion
           myInterfaces.HostName = ipProperties.HostName
           myInterfaces.DomainName = ipProperties.DomainName
           'look at each interface
           For Each networkCard As System.Net.NetworkInformation.NetworkInterface _
               In System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces
               Dim NC As New NetInterfaceClass 'an interface
               'some statistics
               Dim ipstats As System.Net.NetworkInformation.IPv4InterfaceStatistics = _
                       networkCard.GetIPv4Statistics
               NC.BytesRcvd = ipstats.BytesReceived
               NC.BytesSent = ipstats.BytesSent
               NC.InPktErr = ipstats.IncomingPacketsWithErrors
               NC.OutPktErr = ipstats.OutgoingPacketsWithErrors
    
               'information about the interface
               NC.ID = networkCard.Id
               NC.Name = networkCard.Name
               If networkCard.NetworkInterfaceType = _
                       Net.NetworkInformation.NetworkInterfaceType.Loopback Then NC.isLoop = True 'loopback
               NC.Description = networkCard.Description
               NC.Status = networkCard.OperationalStatus.ToString
               NC.SpeedInMBPS = networkCard.Speed \ 1000000
               NC.MAC = networkCard.GetPhysicalAddress.ToString
               NC.InterfaceType = networkCard.NetworkInterfaceType.ToString
               'DNS info
               Dim IPProp As Net.NetworkInformation.IPInterfaceProperties = _
                       networkCard.GetIPProperties
               If Not IPProp Is Nothing Then
                   NC.DNS_Enabled = IPProp.IsDnsEnabled
                   NC.DNS_Dynamic = IPProp.IsDynamicDnsEnabled
               End If
               'dhcp / mtu / wins / forwarding / automatic private
               Dim IPv4 As Net.NetworkInformation.IPv4InterfaceProperties = _
                       networkCard.GetIPProperties.GetIPv4Properties
               If Not IPv4 Is Nothing Then
                   NC.DHCP_Enabled = IPv4.IsDhcpEnabled
                   NC.MTU = IPv4.Mtu
                   NC.UsesWins = IPv4.UsesWins
                   NC.IsForwarding = IPv4.IsForwardingEnabled
                   NC.IsAutomaticPrivateAddressingActive = IPv4.IsAutomaticPrivateAddressingActive
               End If
               NC.IPAddr = New List(Of IP_AddressClass)
               NC.GWAddr = New List(Of GW_AddressClass)
               NC.DNSAddr = New List(Of DNS_AddressClass)
               'IP Address and mask, etc
               For Each ipADDR As Net.NetworkInformation.UnicastIPAddressInformation In _
                       networkCard.GetIPProperties().UnicastAddresses
                   If ipADDR.Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
                       Dim t As New IP_AddressClass
                       t.Address = ipADDR.Address.ToString
                       If Not (ipADDR.IPv4Mask Is Nothing) Then _
                               t.Mask = ipADDR.IPv4Mask.ToString Else t.Mask = ""
                       If NC.DHCP_Enabled Then
                           t.DHCPLeaseExpireUTC = _
                               DateTime.UtcNow + TimeSpan.FromSeconds(ipADDR.DhcpLeaseLifetime)
                       Else
                           t.DHCPLeaseExpireUTC = DateTime.MaxValue
                       End If
                       NC.IPAddr.Add(t)
                   End If
               Next
               'Gateway Address
               For Each gatewayADDR As Net.NetworkInformation.GatewayIPAddressInformation In _
                       networkCard.GetIPProperties.GatewayAddresses
                   If gatewayADDR.Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
                       Dim t As New GW_AddressClass
                       t.Address = gatewayADDR.Address.ToString
                       NC.GWAddr.Add(t)
                   End If
               Next
               'DNS Address
               For Each DNSaddr As Net.IPAddress In _
                       networkCard.GetIPProperties.DnsAddresses
                   If DNSaddr.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
                       Dim t As New DNS_AddressClass
                       t.Address = DNSaddr.ToString
                       NC.DNSAddr.Add(t)
                   End If
               Next
               myInterfaces.Add(NC) 'add interface to list
           Next
           Return myInterfaces
       End Function
       Private Function IPtoNum(ByVal theIP As Net.IPAddress) As Long 'convert IP to number
           Dim IPb() As Byte = theIP.GetAddressBytes 'get the octets
           Dim addr As Long 'accumulator for address
           For x As Integer = 0 To 3
               addr = addr Or (CLng(IPb(x)) << (3 - x) * 8)
           Next
           Return addr
       End Function
       Class AllInterfacesClass
           Public interfaceList As New List(Of NetInterfaceClass)
           Public MemoryTotalMB As ULong
           Public MemoryAvailableMB As ULong
           Public OS As String
           Public HostName As String
           Public DomainName As String
           Public IP_FacingInternet As String
           Public Sub Add(ByVal anInterface As NetInterfaceClass)
               Me.interfaceList.Add(anInterface)
           End Sub
       End Class
       Class NetInterfaceClass
           Public ID As String
           Public Name As String
           Public Description As String
           Public Status As String
           Public SpeedInMBPS As Long
           Public InterfaceType As String
           Public BytesSent As Long
           Public BytesRcvd As Long
           Public InPktErr As Long
           Public OutPktErr As Long
           Public MAC As String
           Public isLoop As Boolean = False
           Public DNS_Enabled As Boolean = False
           Public DNS_Dynamic As Boolean = False
           Public DHCP_Enabled As Boolean = False
           Public MTU As Integer = -1
           Public UsesWins As Boolean = False
           Public IsForwarding As Boolean = False
           Public IsAutomaticPrivateAddressingActive As Boolean = False
           Public IPAddr As New List(Of IP_AddressClass)
           Public GWAddr As New List(Of GW_AddressClass)
           Public DNSAddr As New List(Of DNS_AddressClass)
       End Class
       Class IP_AddressClass
           Public Address As String
           Public Mask As String
           Public Broadcast As String
           Public DHCPLeaseExpireUTC As DateTime
       End Class
       Class GW_AddressClass
           Public Address As String
       End Class
       Class DNS_AddressClass
           Public Address As String
       End Class
    
    End Module
    Last edited by dbasnett; Mar 26th, 2010 at 07:51 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    16

    Re: [02/03] How to get Local IP Address, Subnet and Gateway?

    im having trouble with the declaration of
    Dim ipProperties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties

    wat shud i do?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: [02/03] How to get Local IP Address, Subnet and Gateway?

    You should open your MSDN documentation and read about the IPGlobalProperties class. What assembly and namespace is it declared in? Have to referenced the required assembly and imported the required namespace?

    EDIT: Ah, just noticed that you're using .NET 1.x. IPGlobalProperties was added in .NET 2.0. You could upgrade to VS 2005 or 2008 or you'll probably have to use WMI or unmanaged code.

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    Re: [02/03] How to get Local IP Address, Subnet and Gateway?

    Or you can create a process to run ipconfig and redirect the output back to your app, then extract the information.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width