Reputation: 240
I am creating a Winforms App to view certain details of the different network adapters and connections. I am using the "Win32_NetworkAdapter" and "Win32_NetworkAdapterConfiguration" classes.
One of the features is to display the current status of the adapter whether it is Enabled or Disabled. And using that info I can Enable or Disable the adapter.
The problem I have is that when the adapter is Enabled and connected it works fine, but when the adapter is disabled or when the cable is disconnected it shows as disabled.
How can I specifically check for an Enabled adapter with an unplugged network cable like you would see inside the Windows Network and Sharing center >>> Adapter Settings.
Upvotes: 3
Views: 1679
Reputation: 8104
Used this in one project, it was in 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;
}
}
}
Upvotes: 3