C # network adapter cable disconnected or adapter disconnected

I am building a Winforms application to view some details of various network adapters and connections. I am using the "Win32_NetworkAdapter" and "Win32_NetworkAdapterConfiguration" classes.

One of the features is the display of the current state of the adapter, whether it is enabled or disabled. And using this information, I can enable or disable the adapter.

The problem is that when the adapter is turned on and plugged in, it works fine, but when the adapter is unplugged or when the cable is unplugged, it shows as unplugged.

How can I specifically test the adapter Enabled with the network cable disabled, as you saw in Windows Network and Sharing Center ->> Adapter Settings.

+3


source to share


1 answer


Used this in one project, it was on Windows XP times ...



System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher("SELECT NetConnectionStatus FROM Win32_NetworkAdapter");
foreach (System.Management.ManagementObject networkAdapter in searcher.Get())
{
    if (networkAdapter["NetConnectionStatus"] != null)
    {
        if (Convert.ToInt32(networkAdapter["NetConnectionStatus"]).Equals(2))
        {
            connected = true;
            break;
        }
    }
}

      

+2


source







All Articles