本文介绍了Windows 7与XP GetIsNetworkAvailable()的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了代码,用机器上可用的IPv4 NICard填充菜单.它已经在XP机器上进行了测试,而且看起来还不错(它也是在XP上构建的).

I wrote code to populate a menu with the available IPv4 NICards on a machine. It has been tested on an XP machine and it seems all fine and well (it was also built on XP).

我在Windows 7上进行了测试,即使断开了连接,它也始终填充2个IP地址.这是Win7机器的结果:

I had it tested on Windows 7 and it always populated 2 IP address even if one was disconnected. Here are the results for the Win7 machine:

WLAN已连接
局域网已断开连接

观察到:显示正确的WLAN地址,显示不正确的LAN地址(甚至是连接到192.168的另一个网络号,但是菜单中填充的LAN地址是169.254)
预期:显示正确的WLAN地址,不显示LAN(已断开连接)
ipconfig读取LAN的媒体已断开连接"

WLAN Connected
LAN Disconnected

Observed: Correct WLAN address shows, Incorrect LAN address shows (it is even a different network number where it's connected to 192.168 however the LAN address that is populated in the menu is 169.254)
Expected: Correct WLAN address shows, No LAN shows (it is disconnected)
ipconfig reads "Media disconnected" for LAN

WLAN已连接
局域网已连接

观察到:显示正确的WLAN地址,显示正确的LAN地址
预期:显示正确的WLAN地址,显示正确的LAN地址

ipconfig读取正确的地址

WLAN Connected
LAN Connected

Observed: Correct WLAN address shows, Correct LAN address shows
Expected: Correct WLAN address shows, Correct LAN address shows

ipconfig reads correct address

WLAN已断开连接
局域网已连接

观察到:显示正确的WLAN地址,显示正确的LAN地址
预期:未显示WLAN地址(已断开连接),正确的LAN显示
ipconfig读取WLAN的媒体已断开连接"

WLAN Disconnected
LAN Connected

Observed: Correct WLAN address shows, Correct LAN address shows
Expected: No WLAN address shows(it is disconnected), Correct LAN shows
ipconfig reads "Media disconnected" for WLAN

这是代码块:

Here is the code block:

_adapters.Clear();
if (NetworkInterface.GetIsNetworkAvailable())
{
    NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

    foreach (NetworkInterface adapter in networkInterfaces)
    {
        foreach (UnicastIPAddressInformation addr in adapter.GetIPProperties().UnicastAddresses)
        {
            //This filters out IPv6 and Loopback NICs
            if (addr.Address.AddressFamily == AddressFamily.InterNetwork
                && adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback) 
            {    //This formats something like: 192.168.1.0 - Ethernet adapter Local Network Connection
                _adapters.Add(addr.Address.ToString() + " - " + adapter.NetworkInterfaceType.ToString() + " adapter " + adapter.Name);
            }
        }
    }
}

在VS2010上使用4.0 .NET的价值

Using 4.0 .NET on VS2010 for what its worth

推荐答案

要查看是否已连接NIC,应检查NetworkInterface.OperationalStatus属性.

To see if a NIC is connected or not you should check the NetworkInterface.OperationalStatus property.

LAN断开连接时,奇怪的" IP地址来自APIPA(自动专用IP寻址). Windows Vista中引入的功能":

The "strange" IP address when LAN is disconnected comes from APIPA (Automatic Private IP Addressing). A "feature" introduced with Windows Vista:

这篇关于Windows 7与XP GetIsNetworkAvailable()的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 23:59