【问题标题】:WMI: Getting Interface Name,State NetworkWMI:获取接口名称、状态网络
【发布时间】:2023-04-07 12:22:01
【问题描述】:

美好的一天。

(netsh interface show interface)中有4个分类栏

  1. 管理状态(启用)

  2. 状态(断开)或(连接)

  3. 类型(专用)

  4. 接口名称(以太网 2)或(Wi-Fi)或(以太网)

问题 1: 如何获取我的接口名称 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;
}

我在这里有我想输出的图像。 Sample Image

谢谢你们。

【问题讨论】:

    标签: c# networking


    【解决方案1】:

    您可能希望通过NetConnectionStatus == 2 进行过滤。

    网络连接状态

    • 已断开连接 (0)
    • 连接 (1)
    • 已连接 (2)
    • 断开连接 (3)
    • 硬件不存在 (4)
    • 硬件已禁用 (5)
    • 硬件故障 (6)
    • 媒体已断开连接 (7)
    • 身份验证 (8)
    • 身份验证成功 (9)
    • 身份验证失败 (10)
    • 地址无效 (11)
    • 需要凭据 (12)

    可用属性列表及其可能的值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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多