有
我希望得到一个结果,如果我的Linux设备上有无线适配器,可以通过C#/ mono。
如果我使用命令" iwconfig"如果连接了无线适配器,结果如下:
lo no wireless extensions.
eth0 no wireless extensions.
wlan0 IEEE 802.11bg ESSID:off/any
Mode:Managed Access Point: Not-Associated
Encryption key:off
Power Management:on
如果没有附上,结果是:
lo no wireless extensions.
eth0 no wireless extensions.
我想 System.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType 来获取它,但似乎对于" eth0"和" wlan0",返回结果都是"以太网"。
所以我的问题是有没有其他方法来区分无线适配器?谢谢。
答案 0 :(得分:2)
您可以执行以下操作之一:
/proc/net/wireless
。SIOCGIWNAME
,传递接口名称。#2的可能解决方案是:
using System;
using System.Net.NetworkInformation;
using System.Net.Sockets;
public class App
{
public static int Main (string[] args)
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Unspecified);
foreach(NetworkInterface nic in nics)
{
byte[] name = System.Text.Encoding.ASCII.GetBytes(nic.Description);
byte[] request = new byte[32];
Array.Copy(name, request, name.Length);
bool wireless;
try
{
// SIOCGIWNAME is 0x8b01
socket.IOControl(0x8b01, request, request);
wireless = true;
} catch (SocketException) {
wireless = false;
}
Console.WriteLine("{0} wireless={1}", nic.Description, wireless);
}
return 0;
}
}
答案 1 :(得分:0)
我有另一个答案,也许不是官方答案,但确实有效。
private Process _proCheckWLANInterface;
private bool CheckWirelessAdapter()
{
bool flag = false;
InitProcess(out _proCheckWLANInterface,"iwconfig","");
string strResults = executeCmd(_proCheckWLANInterface);
var datas = strResults.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in datas)
{
if (item.Contains("IEEE 802.11"))
{
flag= true;
break;
}
else
{
logger.Warn("Does not attached Wi-Fi Radio");
}
}
return flag;
}
private void InitProcess(out Process pro,string cmd, string args)
{
pro = new Process();
pro.StartInfo = new ProcessStartInfo();
pro.StartInfo.FileName = cmd;
pro.StartInfo.Arguments = args;
pro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pro.StartInfo.RedirectStandardOutput = true;
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.CreateNoWindow = true;
}
private string executeCmd(Process pro)
{
try
{
pro.Start();
pro.WaitForExit();
//logger.Trace(pro.StandardOutput.ReadToEnd());
return pro.StandardOutput.ReadToEnd();
}
catch (Exception ex)
{
logger.Debug("Exception in executeCmd method:", ex);
}
finally
{
pro.Close();
pro.Dispose();
pro = null;
}
return string.Empty;
}