vb.net 如何使用vb获取本地IP地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7212892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:41:03  来源:igfitidea点击:

How to get local ip address using vb?

vb.net

提问by Rajkumar Reddy

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.net中获取IP地址。我使用下面的代码来获取本地 ip 地址,但它显示未声明 dns。谁能告诉我代码中的 Dns 是什么

VB Code

VB代码

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

回答by Levite

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.

...显然这不完全是 OP 所要求的,但仅从标题和谷歌链接来看,这应该可以回答来这里的人正在寻找的内容。

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

顺便说一句,GetHostByName()似乎已被弃用,GetHostEntry()就像这样以相同的方式工作,而不会发出警告。

回答by Yahia

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

Dns是命名空间中的一个类,System.Net它提供有关“域名系统”(因此名称Dns)的功能 - 请参阅http://msdn.microsoft.com/en-us/library/system.net.dns.gethostname.aspx

回答by AlphaMale

Use this:

用这个:

HttpContext.Current.Request.UserHostAddres

Hope this helps.

希望这可以帮助。

回答by Deepesh

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

回答by Andreas

I found herea good example to get the own IP.

我在这里找到一个很好的例子来获得自己的 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

回答by abhi

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

This will most likely give IPv6 address

这很可能会提供 IPv6 地址

And

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

This will give IPv4 address

这将提供 IPv4 地址

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

我的 VB 生锈了,所以这里有一个适用于我的 C# 代码。

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.

我希望这有帮助。

回答by bitsmonkey

ips = Dns.GetHostAddresses(hostname)

ips = Dns.GetHostAddresses(主机名)

here is a sample codeMSDN

这是一个示例代码MSDN

回答by Dr. Rajesh Rolen

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

您可以使用以下代码获取系统的本地 IP 地址:

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