How to get default NIC Connection Name

后端 未结 6 1632
刺人心
刺人心 2021-01-01 02:27

IMPORTANT EDIT: Back again on this subject. As you said there should be no default NIC, I\'m trying to understand if there is a way to detect all the NICs t

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-01 03:07

    You can use the WMI class Win32_NetworkAdapter to enumerate all the adapters and it has an Index property which might mean that the one with 0 or 1 as the Index is the default one, or one of the other properties might help to find the default one.

    Something like this maybe:

    Edit: Fixed broken code (this is at least more likely to work). But following what abatishchev said I think you might need to use Win32_NetworkAdapterConfiguration.IPConnectionMetric to find the default adapter...

    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))
              textBox1.Text += name + Environment.NewLine;
    }
    

提交回复
热议问题