public void DisConnectToWlan(WlanInfoItem wlanInfoItem) { if (wlanInfoItem.IsConnected) { wlanInfoItem.WlanInterface.DeleteProfile(wlanInfoItem.Name); } }
private static string GetCipherType(WlanInfoItem wlanItem, out string keytype) { string cipher = string.Empty; keytype = string.Empty; switch (wlanItem.CipherAlgorithmType.ToString()) { case "CCMP": cipher = "AES"; keytype = "passPhrase"; break; case "TKIP": cipher = "TKIP"; keytype = "passPhrase"; break; case "None": cipher = "none"; keytype = ""; break; case "WWEP": cipher = "WEP"; keytype = "networkKey"; break; case "WEP40": cipher = "WEP"; keytype = "networkKey"; break; case "WEP104": cipher = "WEP"; keytype = "networkKey"; break; } return(cipher); }
public bool ConnectToWlan(WlanInfoItem wlanItem, string key, bool isAutoConnect, out string errorMsg) { errorMsg = string.Empty; try { string authenticationType = GetAuthenticationType(wlanItem.AuthAlgorithmType); var cipherType = GetCipherType(wlanItem, out var keytype); if (wlanItem.IsSecurityEnabled && string.IsNullOrEmpty(key)) { errorMsg = ("无法连接网络,密码不能为空!"); return(false); } string profileName = wlanItem.Name; string mac = GetMacByWlanName(profileName); string profileXml; 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>{2}</connectionMode><autoSwitch>false</autoSwitch><MSM><security><authEncryption><authentication>{3}</authentication><encryption>{4}</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>{5}</keyType><protected>false</protected><keyMaterial>{6}</keyMaterial></sharedKey><keyIndex>0</keyIndex></security></MSM></WLANProfile>", profileName, mac, isAutoConnect ? "auto" : " manual", authenticationType, cipherType, 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>{2}</connectionMode><autoSwitch>false</autoSwitch><MSM><security><authEncryption><authentication>{3}</authentication><encryption>{4}</encryption><useOneX>false</useOneX></authEncryption></security></MSM></WLANProfile>", profileName, mac, isAutoConnect ? "auto" : " manual", authenticationType, cipherType); } wlanItem.WlanInterface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true); bool success = wlanItem.WlanInterface.ConnectSynchronously(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName, 15000); return(success); } catch (Exception e) { errorMsg = e.Message; return(false); } }