开发者

How to get local ip address using vb?

开发者 https://www.devze.com 2023-03-31 17:08 出处:网络
How to get ip address in vb.net. i used below code to get local ip address but it showingdns is not declared. can any one tell me what is that Dns in the Code

How to get ip address in vb.net. i used below code to get local ip address but it showing dns is not declared. can any one tell me what is that Dns in the 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开发者_Python百科 = CType(Dns.GetHostByName(hostname).AddressList.GetValue(0), IPAddr
ess).ToString
Console.WriteLine("Computer Name: " & hostname & " IP Address: " & ipaddress)
End Sub

End Class


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

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 just from the title and google links, this should answer what people coming here are looking for.

Btw GetHostByName()seems to be deprecated, GetHostEntry() like this works the same way, without throwing a warning.


Dns is a class in the namespace System.Net which provides functionality regarding the "Domain Name System" (thus the name Dns) - see http://msdn.microsoft.com/en-us/library/system.net.dns.gethostname.aspx


Use this:

HttpContext.Current.Request.UserHostAddres

Hope this helps.


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


ips = Dns.GetHostAddresses(hostname)

here is a sample codeMSDN


I found here a good example to get the own IP.

        Dim _IP As String = Nothing

        Dim _IPHostEntry As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())

        For Each _IPAddress As System.Net.IPAddress In _IPHostEntry.AddressList
            If _IPAddress.AddressFamily.ToString() = "InterNetwork" Then
                _IP = _IPAddress.ToString()
            End If
        Next _IPAddress
        Return _IP


You can get local ip address of system using below code:

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


Dim ipaddress As String =
    Dns.GetHostEntry(Dns.GetHostName).AddressList(0).ToString

This will most likely give IPv6 address

And

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

This will give IPv4 address

My VB is rusty so here is a C# code that works for me.

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

I hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号