如何使用vb获取本地IP地址? [英] How to get local ip address using vb?

查看:36
本文介绍了如何使用vb获取本地IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 vb.net 中获取 IP 地址.我使用下面的代码来获取本地 ip 地址,但它显示未声明 dns.谁能告诉我代码中的 Dns 是什么

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 代码

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

推荐答案

既然我觉得问题(在标题中)还没有完全回答......

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

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

... 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.

顺便说一句 GetHostByName() 好像已经被弃用了,GetHostEntry() 像这样的工作方式是一样的,没有抛出警告.

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

这篇关于如何使用vb获取本地IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆