Kcvin
Kcvin

Reputation: 5163

Windows 7 vs. XP GetIsNetworkAvailable() difference?

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

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 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 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 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);
            }
        }
    }
}

Using 4.0 .NET on VS2010 for what its worth

Upvotes: 1

Views: 701

Answers (1)

Adriano Repetti
Adriano Repetti

Reputation: 67090

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

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

...a feature in Windows Vista to automatically configure itself with an IP address and subnet mask when a DHCP server isn't available. The IP address range is 169.254.0.1 through 169.254.255.254, a range that has been reserved especially for Microsoft.

Upvotes: 2

Related Questions