简体   繁体   中英

WMI: Getting Interface Name,State Network

Good day.

There are 4 category column in (netsh interface show interface)

  1. Admin State (Enabled)

  2. State (Disconnected) or (Connected)

  3. Type (Dedicated)

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

You may want to filter by NetConnectionStatus == 2 .

NetConnectionStatus

  • Disconnected (0)
  • Connecting (1)
  • Connected (2)
  • Disconnecting (3)
  • Hardware Not Present (4)
  • Hardware Disabled (5)
  • Hardware Malfunction (6)
  • Media Disconnected (7)
  • Authenticating (8)
  • Authentication Succeeded (9)
  • Authentication Failed (10)
  • Invalid Address (11)
  • Credentials Required (12)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM