1

那里

我想通过 C#/mono 得到一个结果,如果我的 Linux 设备上有无线适配器。

如果我在连接无线适配器的情况下使用命令“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”,返回结果都是“以太网”。

所以我的问题是有没有其他方法来区分无线适配器?谢谢。

4

2 回答 2

2

您可以执行以下操作之一:

  1. 检查界面是否出现在/proc/net/wireless.
  2. 创建一个套接字并SIOCGIWNAME在其上调用,传递接口名称。
  3. 尝试您想要的任何特定于无线的任务,看看它是否失败。

#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;
        }
}
于 2013-10-14T15:10:23.680 回答
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;
    }
于 2013-10-25T01:00:32.943 回答