在 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() + ;
}
上面的代码给出了以太网 LAN 描述的列表。 甚至 wifi LAN 的 NetworkInterfaceType 也显示为以太网,例如: 类型:以太网 描述:Intel(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;
}
}
任何指导将不胜感激。 谢谢
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");
}
}
}
这将消除除物理有线接口之外的所有接口
NetworkInterface[] 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}");
}
}