如何在vb.net中获取IP地址。我使用下面的代码来获取本地IP地址,但它没有声明显示dns。任何人都可以告诉我代码中的Dns是什么
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
答案 0 :(得分:4)
由于我感觉到,问题(标题中)尚未完全回答......
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
......显然这不是OP所要求的,但只是从标题和谷歌链接,这应该回答人们来这里寻找的东西。
Btw GetHostByName()
似乎已被弃用,GetHostEntry()
就像这样工作方式相同,而不会发出警告。
答案 1 :(得分:2)
Dns
是名称空间System.Net
中的一个类,它提供有关“域名系统”的功能(因此名称为Dns
) - 请参阅http://msdn.microsoft.com/en-us/library/system.net.dns.gethostname.aspx
答案 2 :(得分:1)
使用此:
HttpContext.Current.Request.UserHostAddres
希望这有帮助。
答案 3 :(得分: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
答案 4 :(得分:0)
ips = Dns.GetHostAddresses(主机名)
这是一个示例代码MSDN
答案 5 :(得分:0)
您可以使用以下代码获取系统的本地IP地址:
Dim host As String = System.Net.Dns.GetHostName()
Dim LocalHostaddress As String = System.Net.Dns.GetHostByName(host).AddressList(1).ToString()
答案 6 :(得分: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生锈了,所以这里有一个适合我的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
我希望这会有所帮助。
答案 7 :(得分:0)
我发现here是获得自己IP的好例子。
genreModel.Brand.BrandName