.net 如何在C#中获取忽略Wifi(无线)LAN连接的有线LAN连接列表

xkftehaa  于 2023-01-22  发布在  .NET
关注(0)|答案(2)|浏览(192)

在C#中,我希望在运行时获得笔记本电脑有线LAN连接列表,但我不希望有任何WiFi或无线LAN。
我已经使用了各种方法来获得网络列表:-
ManagementClass

ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
 ManagementObjectCollection objMOC = objMC.GetInstances();

 foreach (ManagementObject objMO in objMOC)
        {
            if (!(bool)objMO["ipEnabled"])
                continue;
           this.textBox1.Text += (Convert.ToString(objMO["Description"])) + Environment.NewLine;
        }

上面的代码没有给出网络类型的信息(wifi或有线)
NetworkInterface类:

foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())

        {
            this.textBox1.Text += nic.Name.ToString();    

            this.textBox1.Text += "MAC: " + nic.GetPhysicalAddress().ToString();

            this.textBox1.Text += "Type: " + nic.NetworkInterfaceType;
            this.textBox1.Text += "Description: " + nic.Description.ToString();
            this.textBox1.Text += "OperationalStatus: " + nic.OperationalStatus.ToString() + ;
        }

上面的代码给了我以太网局域网的描述列表。即使是网络接口类型的无线局域网显示为Ehternet,例如:类型:以太网描述:英特尔®迅驰® Ultimate-N 6300 AGN-数据包调度程序微型端口
我的意思是没有办法区分哪一个是无线或有线网络。
我也试过**NativeWifi.WlanClient**,但它崩溃了。

Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
        foreach (Wlan.WlanAvailableNetwork network in networks)
        {   
            if (network.dot11DefaultCipherAlgorithm == Wlan.Dot11CipherAlgorithm.WEP)
            {
                this.textBox1.Text = string.Format("Found WEP network with SSID {0}: ", GetStringForSSID(network.dot11Ssid)) + Environment.NewLine;
            }
        }

如有任何指导,我们将不胜感激。谢谢

f87krz0w

f87krz0w1#

NetworkInterface[] nics=NetworkInterface.GetAllNetworkInterfaces();

foreach(NetworkInterface adapter in nics)
{
  if(adapter.OperationalStatus==OperationalStatus.Up)
  {
    if(adapter.NetworkInterfaceType==NetworkInterfaceType.Wireless80211)
    {
      Console.WriteLine("Wifi");
    }
    else
    {
      Console.WriteLine("do your work");
    }
  }
}
ffdz8vbo

ffdz8vbo2#

var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
                        foreach (var networkInterface in networkInterfaces)
                        {
                            
                            if (networkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
                            networkInterface.NetworkInterfaceType != NetworkInterfaceType.Wwanpp &&
                            networkInterface.NetworkInterfaceType != NetworkInterfaceType.Wwanpp2 &&
                            networkInterface.NetworkInterfaceType != NetworkInterfaceType.Wireless80211 &&
                            networkInterface.NetworkInterfaceType != NetworkInterfaceType.Unknown &&
                            networkInterface.NetworkInterfaceType != NetworkInterfaceType.HighPerformanceSerialBus &&
                            networkInterface.NetworkInterfaceType != NetworkInterfaceType.Slip &&
                            networkInterface.NetworkInterfaceType != NetworkInterfaceType.Tunnel && 
                            networkInterface.OperationalStatus != OperationalStatus.NotPresent && 
                            !networkInterface.Name.Contains("Bluetooth") && !networkInterface.Description.Contains("Virtual"))
                            {
                                Trace.Write($"Description :{networkInterface.Description}");
                                Trace.WriteLine($"Interface Name: {networkInterface.Name}");

                            }
                        }

这将消除除物理有线接口以外的所有接口

相关问题