我正在尝试从我的代码连接到wifi网络。我发现Managedwifi是个好方法。而且我用它来扫描wifi网络,这很好。但问题是我无法连接到网络,也无法在互联网上找到示例代码。我用谷歌搜索,但没有结果!
请帮助我连接到网络!
这是尝试的代码!

static void Main(string[] args)
    {
        WlanClient client = new WlanClient();
        foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
        {
            // Lists all available networks
            Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
            foreach (Wlan.WlanAvailableNetwork network in networks)
            {
                Console.WriteLine("Found network with SSID {0} || Secured : {1}.", GetStringForSSID(network.dot11Ssid),network.securityEnabled);
            }
            //wlanIface.DeleteProfile("Xperia Arc");
            //string profileName = GetStringForSSID(networks[0].dot11Ssid); // this is also the SSID
            string profileName = "Xperia Arc";
            string mac = "52544131303235572D454137443638";
            string key = "9090090900";
            string profileXml = string.Format("<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>{0}</name><SSIDConfig><SSID><hex>{1}</hex><name>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><MSM><security><authEncryption><authentication>open</authentication><encryption>WEP</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>networkKey</keyType><protected>false</protected><keyMaterial>{2}</keyMaterial></sharedKey><keyIndex>0</keyIndex></security></MSM></WLANProfile>", profileName, mac, key);
            try
            {
                wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
                wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
            }
            catch (Exception)
            {
                Console.WriteLine("Error Occured!");
                //throw;
            }
            while (wlanIface.InterfaceState.ToString()=="Associating")
            {
                ;
            }
            Console.WriteLine(wlanIface.InterfaceState.ToString() + wlanIface.);
            //Console.WriteLine(wlanIface.CurrentConnection.profileName +" "+ wlanIface.InterfaceState.ToString());
        }

    }

    static string GetStringForSSID(Wlan.Dot11Ssid ssid)
    {
        return Encoding.ASCII.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);
    }

最佳答案

有关ManagedWifi的常规用法,请参见:C# Connect to Wifi Network with Managed Wifi API

关于配置文件xml中“ mac”的问题:它的ssid为十六进制表示形式。

您的代码有多个问题:


不要在所有接口上运行并连接到同一wifi网络,只需使用第一个可用接口
您的while循环等待接口状态“ Association”将不起作用,因为通常接口仍将处于“ Disconnected”状态。因此最好等到“已连接”或超时。


因此,您的代码应如下所示:

static void Main(string[] args)
    {
        WlanClient client = new WlanClient();
        WlanClient.WlanInterface wlanIface = client.Interfaces.FirstOrDefault();

        if(wlanIface == null)
        {
            Console.WriteLine("No Wifi Interface available!");
            throw new Exception("No Wifi Interface available!");
        }

        // Lists all available networks
        Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
        foreach (Wlan.WlanAvailableNetwork network in networks)
        {

            Console.WriteLine("Found network with SSID {0} || Secured : {1}.", GetStringForSSID(network.dot11Ssid),network.securityEnabled);
        }

        string profileName = "Xperia Arc";
        string ssid = profileName;
        byte[] ssidBytes = Text.Encoding.Default.GetBytes(ssid);
        string ssidHex = BitConverter.ToString(ssidBytes);
        ssidHex = ssidHex.Replace("-", "");
        string key = "9090090900";

        string profileXml = string.Format("<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>{0}</name><SSIDConfig><SSID><hex>{1}</hex><name>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><MSM><security><authEncryption><authentication>open</authentication><encryption>WEP</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>networkKey</keyType><protected>false</protected><keyMaterial>{2}</keyMaterial></sharedKey><keyIndex>0</keyIndex></security></MSM></WLANProfile>", ssid, ssidHex, key);

        try
        {
            wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
            wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
        }
        catch (Exception)
        {
            Console.WriteLine("Error Occured!");
            //throw;
        }

        int timeout = 3000;
        while ((wlanIface.InterfaceState.ToString()<>"Connected") && (timeout >= 0))
        {
               System.Threading.Thread.Sleep(500);
               timeout -= 500;
        }

        Console.WriteLine(wlanIface.InterfaceState.ToString() + wlanIface);
        //Console.WriteLine(wlanIface.CurrentConnection.profileName +" "+ wlanIface.InterfaceState.ToString());
    }

关于c# - 使用ManagedWifi连接到C#中的wifi,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26887319/

10-17 01:02