如何在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
既然我觉得这个问题(标题中)还没有完全回答……
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()以相同的方式工作,而不会发出警告.