Reputation: 59
Good day.
There are 4 category column in (netsh interface show interface)
Admin State (Enabled)
State (Disconnected) or (Connected)
Type (Dedicated)
Interface Name (Ethernet 2) or (Wi-Fi) or (Ethernet)
Question 1: How to get my interface Name where State = "Connected";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapter");
foreach (ManagementObject mo in mc.GetInstances())
{
int index = Convert.ToInt32(mo["Index"]);
string name = mo["NetConnectionID"] as string;
if (!string.IsNullOrEmpty(name))
MessageBox.Show(name);
//textBox1.Text += name + Environment.NewLine;
}
I have here image which i want to be the output. Sample Image
Thank you guys.
Upvotes: 3
Views: 2889
Reputation: 4125
You may want to filter by NetConnectionStatus == 2
.
NetConnectionStatus
A list of the available Properties and their possible values can be found in the MSDN entry for Win32_NetworkAdapter.
var mc = new ManagementClass("Win32_NetworkAdapter");
mc.GetInstances()
.OfType<ManagementObject>()
.Where(mo => !string.IsNullOrEmpty(mo["NetConnectionID"] as string)) // has a ConnectionId
.ToList()
.ForEach(mo => Debug.WriteLine($"NetConnectionStatus = {mo["NetConnectionStatus"]} / NetConnectionID={mo["NetConnectionID"]} / Name={mo["Name"]}"));
//Result:
// NetConnectionStatus=7 / NetConnectionID=Ethernet / Name=Intel(R) Ethernet Connection (5) I219-LM
// NetConnectionStatus=7 / NetConnectionID=WiFi / Name=Intel(R) Dual Band Wireless-AC 8265
// NetConnectionStatus=7 / NetConnectionID=Bluetooth Network Connection / Name=Bluetooth Device (Personal Area Network)
// NetConnectionStatus=2 / NetConnectionID=VMware Network Adapter VMnet1 / Name=VMware Virtual Ethernet Adapter for VMnet1
// NetConnectionStatus=2 / NetConnectionID=VMware Network Adapter VMnet8 / Name=VMware Virtual Ethernet Adapter for VMnet8
Upvotes: 4