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

时间:2011-12-21 06:09:53

标签: .net wifi wireless lan

在C#中,我希望在运行时获取笔记本电脑的有线局域网连接列表,但我不想要任何wifi或无线局域网。

我已经使用各种方式获取网络列表: -

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() + ;
        }

上面的代码给出了以太网LAN描述的列表。 甚至用于wifi LAN的NetworkInterfaceType显示为Ehternet,例如:    类型:以太网    描述:英特尔(R)Centrino(R)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;
            }
        }

任何指导将不胜感激。 感谢

1 个答案:

答案 0 :(得分:3)

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");
    }
  }
}