How to get local IP address using vb?

How to get ip address in vb.net. I used the code below to get the local IP address, but it shows that dns is not advertised. can anyone tell me what dns is in code

VB Code

Imports System.Environment
Imports System.Net

Public Class Tester
Public Shared Sub Main
Dim hostname As String = Dns.GetHostName()
Dim ipaddress As String = CType(Dns.GetHostByName(hostname).AddressList.GetValue(0), IPAddr
ess).ToString
Console.WriteLine("Computer Name: " & hostname & " IP Address: " & ipaddress)
End Sub

End Class
+5
source share
7 answers

Since I get the feeling that the question (in the title) has not yet fully answered ...

Dim hostName = System.Net.Dns.GetHostName()
For Each hostAdr In System.Net.Dns.GetHostEntry(hostName).AddressList()

    ' If you just want to write every IP
    Console.WriteLine("Name: " & hostName & " IP Address: " & hostAdr.ToString() 

    ' If you want to look if the device is member of a specific network
    If hostAdr.ToString().StartsWith("192.168.1.") Then DoSomething() : Exit For

    ' I think you get the idea ^^
    ' ...
Next

... obviously, this is not exactly what the OP asked for, but only from the name and google links, this should answer what people who are looking for are looking for it.

Btw GetHostByName()seems deprecated, GetHostEntry()as it works the same, without warning.

+3
source
+2

:

HttpContext.Current.Request.UserHostAddres

, .

+1
Dim hostName = System.Net.Dns.GetHostName()
    For Each hostAdr In System.Net.Dns.GetHostEntry(hostName).AddressList()

        ' If you just want to write every IP
        'Console.WriteLine("Name: " & hostName & " IP Address: " & hostAdr.ToString())
        Me.RichTextBox1.Text = hostAdr.ToString

        'If you want to look if the device is member of a specific network

        ' ...
    Next
+1

ips = Dns.GetHostAddresses( )

MSDN

0

IP- , :

Dim host As String = System.Net.Dns.GetHostName()
Dim LocalHostaddress As String = System.Net.Dns.GetHostByName(host).AddressList(1).ToString()
0
Dim ipaddress As String =
    Dns.GetHostEntry(Dns.GetHostName).AddressList(0).ToString

, , IPv6

Dim ipaddress As String =
    Dns.GetHostEntry(Dns.GetHostName).AddressList.FirstOrDefault(() => { },
    (ip.AddressFamily = AddressFamily.InterNetwork)).ToString

IPv4

VB , #, .

using System.Linq;

using System.Net.Sockets;

string IPaddress =
    Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString(); //For Ipv6

string IPaddress =
    Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip =>
    ip.AddressFamily == AddressFamily.InterNetwork).ToString(); //For Ipv4

, .

0

All Articles