简体   繁体   English

通过C#获取网络接口类型

[英]Get Type of network interface via C#

I am querying the Network Interfaces of a Computer via C# as follows: 我通过C#查询计算机的网络接口,如下所示:

var interfaces = new List<string>();

var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
var moc = mc.GetInstances();

foreach (ManagementObject mo in moc)
{
    if (!(bool)mo["ipEnabled"])
        continue;

    string desc = (string) (mo["Description"]);

    interfaces.Add(desc);
 }

 return interfaces;

This Returns a list with Network Interfaces. 这将返回包含网络接口的列表。 The problem is that this contains the names of the Interfaces (Broadcom Family ...). 问题是它包含接口的名称(Broadcom Family ...)。 But my users do not understand what kind of Interface this is (Ethernet, wifi, ...). 但我的用户不明白这是什么类型的接口(以太网,wifi,...)。 Is it possible to figure out what type of a network interface this is? 是否有可能弄清楚这是什么类型的网络接口? How would I do this? 我该怎么做?

Not sure what you using to get all network interfaces but in .NET we have NetworkInterface class in System.Net.NetworkInformation namespace . 不确定您使用什么来获取所有网络接口,但在.NET中我们在System.Net.NetworkInformation namespaceNetworkInterface class

You can get all interfaces with method NetworkInterface.GetAllNetworkInterfaces() . 您可以使用方法NetworkInterface.GetAllNetworkInterfaces()获取所有接口。

Then to understand what kind of interface you get you can check NetworkInterfaceType property. 然后,要了解您获得的接口类型,可以检查NetworkInterfaceType属性。 It's enum so for your purpose you can do something like this: 这是enum所以对于你的目的,你可以这样做:

foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
{
    if (netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 
        || netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
    {
        //your logic here
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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