在VB.net中查找IP地址(XP与Windows 7) [英] IP Address Lookup in VB.net (XP vs Windows 7)

查看:131
本文介绍了在VB.net中查找IP地址(XP与Windows 7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我使用以下代码来检索本地工作站的IP地址...

Currently I use the following code to retrieve the IP address of the local workstation...

strIPAddress = System.Net.Dns.GetHostEntry(strComputerName).AddressList(0).ToString()

这对于Windows XP工作站很好.但是,在Vista和Windows 7中,这将返回根本不使用的IPv6地址. 是否有一种方法可以使其正常工作,因此无论平台如何,它总是返回IPv4地址?

This is fine for the Windows XP workstations. However, in Vista and Windows 7, this returns the IPv6 address which is not used at all. Is there a method of setting this to work so it always returns the IPv4 address regardless of platform?

我知道我可以将AddressList值增加到1并在Windows 7中获得正确的IP.糟糕的是,这需要经历识别操作系统并选择一个或另一个的动作.

I know I can increment the AddressList value to 1 and get the correct IP in Windows 7. The bad part is that this requires going through the motions of identifying the OS and choosing one or the other.

必须是仅指定IPv4的某种方式.也许从网络上的DNS而不是工作站本身获得结果?

The must be some way of specifying IPv4 only. Perhaps getting a result from DNS on the network rather than the workstation itself?

推荐答案

您只需要遍历AddressList并查看AddressFamily即可看到将其设置为InterNetwork

You just need to loop through the AddressList looking at the AddressFamily seeing which is set to InterNetwork

Dim IP4 = New List(Of IPAddress)(Dns.GetHostEntry(strComputer).AddressList).Find(Function(f) f.AddressFamily = Sockets.AddressFamily.InterNetwork)

或者更长一点:

    Dim IP4 As IPAddress
    Dim AL = Dns.GetHostEntry(strComputer).AddressList
    For Each A In AL
        If A.AddressFamily = Sockets.AddressFamily.InterNetwork Then
            IP4 = A
            Exit For
        End If
    Next

这篇关于在VB.net中查找IP地址(XP与Windows 7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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