public static bool connectViaXml(WIFISSID ssid, string profileName, string profileXml) { ssid.wlanInterface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true); //ssid.wlanInterface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, ssid.profileNames); bool success = ssid.wlanInterface.ConnectSynchronously(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName, 15000); return(success); }
public static List <WIFISSID> ScanAllSSID() { List <WIFISSID> wifiList = new List <WIFISSID>(); string conectedNetworkName = string.Empty; foreach (WlanClient.WlanInterface wlanIface in client.Interfaces) { List <string> profileNames = new List <string>(); // Lists all networks with WEP security Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0); foreach (Wlan.WlanAvailableNetwork network in networks) { if (wlanIface.InterfaceState == Wlan.WlanInterfaceState.Connected && wlanIface.CurrentConnection.isState == Wlan.WlanInterfaceState.Connected) { conectedNetworkName = wlanIface.CurrentConnection.profileName; } WIFISSID targetSSID = new WIFISSID(); if (network.networkConnectable) { targetSSID.SSID = network.dot11Ssid; if (string.IsNullOrEmpty(network.profileName)) { targetSSID.profileNames = GetStringForSSID(network.dot11Ssid); } else { targetSSID.profileNames = network.profileName; } if (!profileNames.Contains(targetSSID.profileNames)) { profileNames.Add(targetSSID.profileNames); targetSSID.wlanInterface = wlanIface; targetSSID.wlanSignalQuality = (int)network.wlanSignalQuality; targetSSID.dot11DefaultAuthAlgorithm = network.dot11DefaultAuthAlgorithm; targetSSID.dot11DefaultCipherAlgorithm = network.dot11DefaultCipherAlgorithm; targetSSID.securityEnabled = network.securityEnabled; wifiList.Add(targetSSID); if (!string.IsNullOrEmpty(conectedNetworkName) && conectedNetworkName.Equals(network.profileName)) { targetSSID.connected = true; } else { targetSSID.connected = false; } } } } } return(wifiList); }
/// <summary> /// 连接到无线网络 /// </summary> /// <param name="ssid"></param> public static bool ConnectToSSID(WIFISSID ssid, string key) { try { String auth = string.Empty; String cipher = string.Empty; bool isNoKey = false; String keytype = string.Empty; switch (ssid.dot11DefaultAuthAlgorithm) { case Wlan.Dot11AuthAlgorithm.IEEE80211_Open: auth = "open"; break; //case Wlan.Dot11AuthAlgorithm.IEEE80211_SharedKey: // 'not implemented yet; //break; case Wlan.Dot11AuthAlgorithm.RSNA: auth = "WPA2PSK"; break; case Wlan.Dot11AuthAlgorithm.RSNA_PSK: auth = "WPA2PSK"; break; case Wlan.Dot11AuthAlgorithm.WPA: auth = "WPAPSK"; break; case Wlan.Dot11AuthAlgorithm.WPA_None: auth = "WPAPSK"; break; case Wlan.Dot11AuthAlgorithm.WPA_PSK: auth = "WPAPSK"; break; } switch (ssid.dot11DefaultCipherAlgorithm) { case Wlan.Dot11CipherAlgorithm.CCMP: cipher = "AES"; keytype = "passPhrase"; break; case Wlan.Dot11CipherAlgorithm.TKIP: cipher = "TKIP"; keytype = "passPhrase"; break; case Wlan.Dot11CipherAlgorithm.None: cipher = "none"; keytype = ""; isNoKey = true; break; case Wlan.Dot11CipherAlgorithm.WEP: cipher = "WEP"; keytype = "networkKey"; break; case Wlan.Dot11CipherAlgorithm.WEP40: cipher = "WEP"; keytype = "networkKey"; break; case Wlan.Dot11CipherAlgorithm.WEP104: cipher = "WEP"; keytype = "networkKey"; break; } if (isNoKey && !string.IsNullOrEmpty(key)) { // infoTB.Text = "无法连接网络!"; Loger.WriteLog("无法连接网络", "SSID:" + ssid.SSID + "\r\n" + "Dot11AuthAlgorithm:" + ssid.dot11DefaultAuthAlgorithm + "\r\n" + "Dot11CipherAlgorithm:" + ssid.dot11DefaultAuthAlgorithm.ToString()); return(false); } else if (!isNoKey && string.IsNullOrEmpty(key)) { // infoTB.Text = "无法连接网络!"; Loger.WriteLog("无法连接网络", "SSID:" + ssid.SSID + "\r\n" + "Dot11AuthAlgorithm:" + ssid.dot11DefaultAuthAlgorithm + "\r\n" + "Dot11CipherAlgorithm:" + ssid.dot11DefaultAuthAlgorithm.ToString()); return(false); } else { string profileName = ssid.profileNames; // this is also the SSID string mac = StringToHex(profileName); string profileXml = string.Empty; if (!string.IsNullOrEmpty(key)) { 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><connectionMode>auto</connectionMode><autoSwitch>false</autoSwitch><MSM><security><authEncryption><authentication>{2}</authentication><encryption>{3}</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>{4}</keyType><protected>false</protected><keyMaterial>{5}</keyMaterial></sharedKey><keyIndex>0</keyIndex></security></MSM></WLANProfile>", profileName, mac, auth, cipher, keytype, key); } else { 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><connectionMode>auto</connectionMode><autoSwitch>false</autoSwitch><MSM><security><authEncryption><authentication>{2}</authentication><encryption>{3}</encryption><useOneX>false</useOneX></authEncryption></security></MSM></WLANProfile>", profileName, mac, auth, cipher, keytype); } bool success = connectViaXml(ssid, profileName, profileXml); if (!success) { //infoTB.Text = "连接网络失败!"; Loger.WriteLog("连接网络失败", "SSID:" + ssid.SSID + "\r\n" + "Dot11AuthAlgorithm:" + ssid.dot11DefaultAuthAlgorithm + "\r\n" + "Dot11CipherAlgorithm:" + ssid.dot11DefaultAuthAlgorithm.ToString() + "\r\n"); return(false); } } } catch (Exception e) { // infoTB.Text = "无法连接网络!" + e.Message; Loger.WriteLog("无法连接网络", "SSID:" + ssid.SSID + "\r\n" + "Dot11AuthAlgorithm:" + ssid.dot11DefaultAuthAlgorithm + "\r\n" + "Dot11CipherAlgorithm:" + ssid.dot11DefaultAuthAlgorithm.ToString() + "\r\n" + e.Message); return(false); } return(true); }