如何使用VB.NET区分以太网和WiFi IP地址 [英] How to distinguish between Ethernet and WiFi IP Addresses using VB.NET

查看:296
本文介绍了如何使用VB.NET区分以太网和WiFi IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个基于VB.NET的通用应用程序,该应用程序将向我返回本地计算机的以太网IP地址。我已经提到了这里讨论的获取机器IP地址的几个问题,并找到了一些很好的建议。

I am trying to develop a generic VB.NET based application that will return me the Ethernet IP Address of the local machine. I have referred to several questions discussed here for getting the IP Address of the machine and found a few good suggestions.

我面临的问题是,当我运行此应用程序时,它会向我返回WiFi和以太网的IP地址。当我在别人的机器上运行该应用程序时,我无法分辨出哪个IP地址属于哪个接口。我只对以太网IP地址感兴趣。

The problem I am facing is, when I run this application, it returns me the IP Address of both WiFi and Ethernet. When I run this application on somebody else's machine, I unable to tell which IP Address belongs to which interface. I am interested in Ethernet IP Address only.

任何建议??

以下是返回IP地址列表。

Here is the function that returns the list of IP Addresses.

Function getIP() As String

    Dim ips As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)

    For Each ip In ips.AddressList
        If (ip.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork) Then
            MessageBox.Show(ip.ToString)
            Return ip.ToString
        End If
    Next
    Return Nothing

End Function


推荐答案

而不是通过<$ c获取IP地址$ c> IPHostEntry ,您可以通过网络适配器进行枚举,然后从每个适配器获取IP地址。

Rather than getting IP addresses via an IPHostEntry, you can enumerate through the network adapters, then get the IP addresses from each adapter.

A NetworkInterface 通过 NetworkInterfaceType 属性提供其类型。对于以太网适配器,这将返回 Ethernet 。对于无线适配器,文档未指定,但是为我返回了 Wireless80211

A NetworkInterface provides its type via the NetworkInterfaceType property. For ethernet adapters, this returns Ethernet. For a wireless adapter, the documentation doesn't specify, but it returned Wireless80211 for me.

示例代码:

Imports System.Net.NetworkInformation


Public Class Sample

    Function GetIP() As String
        Dim networkInterfaces() As NetworkInterface


        networkInterfaces = NetworkInterface.GetAllNetworkInterfaces()

        For Each networkInterface In networkInterfaces
            If networkInterface.NetworkInterfaceType = NetworkInterfaceType.Ethernet Then
                For Each address In networkInterface.GetIPProperties().UnicastAddresses
                    If address.Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
                        Return address.Address.ToString()
                    End If
                Next address
            End If
        Next networkInterface

        Return Nothing
    End Function

End Class

或者,如果您想要一个席位更简洁的版本,您可以使用LINQ(相当于上面的代码):

Or, if you want a slightly more concise version, you could use LINQ (equivalent to the code above):

Function GetIP() As String
    Return (
        From networkInterface In networkInterface.GetAllNetworkInterfaces()
        Where networkInterface.NetworkInterfaceType = NetworkInterfaceType.Ethernet
        From address In networkInterface.GetIPProperties().UnicastAddresses
        Where address.Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork
        Select ip = address.Address.ToString()
    ).FirstOrDefault()
End Function    

这篇关于如何使用VB.NET区分以太网和WiFi IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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