How to use C # to operate a wireless network card to connect to a wireless network has been a major confusion in my learning process. Fortunately, this problem has been successfully solved recently. Recently, I was writing a ChinaNet wireless hotspot Automatic Connection Tool for China Telecom, and used the Managed Wifi API, which is very convenient to use.
The procedure is simple:
1.Download Managed Wifi API
About Managed Wifi API: This project is a. NET class library allowing you to control Wifi (802.11) network adapters installed in your Windows machine programmatically.
The library uses the Native Wifi API, available since Windows Vista and Windows XP SP2 (in a limited fashion, and only after applying a hotfix provided in KB article 918997 ). older versions of Windows are not supported.
2.Create a C # project file and add a reference to ManagedWifi. dll.
3.Write code and reference "Native Wifi API ".
The key code is as follows:
using System;
using System.Collections.Generic;
using System.Text;
using NativeWifi;
namespace ManagedWifiExample
{
class MyWifi
{
public List <WIFISSID> ssids = new List <WIFISSID> ();
public MyWifi ()
{
ssids.Clear ();
}
static string GetStringForSSID (Wlan.Dot11Ssid ssid)
{
return Encoding.UTF8.GetString (ssid.SSID, 0, (int) ssid.SSIDLength);
}
/// <summary>
/// Enumerate the SSID received by all wireless devices
/// </ summary>
public void ScanSSID ()
{
WlanClient client = new WlanClient ();
foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
{
// Lists all networks with WEP security
Wlan.WlanAvailableNetwork [] networks = wlanIface.GetAvailableNetworkList (0);
foreach (Wlan.WlanAvailableNetwork network in networks)
{
WIFISSID targetSSID = new WIFISSID ();
targetSSID.wlanInterface = wlanIface;
targetSSID.wlanSignalQuality = (int) network.wlanSignalQuality;
targetSSID.SSID = GetStringForSSID (network.dot11Ssid);
//targetSSID.SSID = Encoding.Default.GetString (network.dot11Ssid.SSID, 0, (int) network.dot11Ssid.SSIDLength);
targetSSID.dot11DefaultAuthAlgorithm = network.dot11DefaultAuthAlgorithm.ToString ();
targetSSID.dot11DefaultCipherAlgorithm = network.dot11DefaultCipherAlgorithm.ToString ();
ssids.Add (targetSSID);
// if (network.dot11DefaultCipherAlgorithm == Wlan.Dot11CipherAlgorithm.WEP)
// {
// Console.WriteLine ("Found WEP network with SSID {0}.", GetStringForSSID (network.dot11Ssid));
//}
//Console.WriteLine("Found network with SSID {0}. ", GetStringForSSID (network.dot11Ssid));
//Console.WriteLine("dot11BssType:{0}. ", Network.dot11BssType.ToString ());
//Console.WriteLine("dot11DefaultAuthAlgorithm:{0}. ", Network.dot11DefaultAuthAlgorithm.ToString ());
//Console.WriteLine("dot11DefaultCipherAlgorithm:{0}. ", Network.dot11DefaultCipherAlgorithm.ToString ());
//Console.WriteLine("dot11Ssid:{0}. ", Network.dot11Ssid.ToString ());
//Console.WriteLine("flags:{0}. ", Network.flags.ToString ());
//Console.WriteLine("morePhyTypes:{0}. ", Network.morePhyTypes.ToString ());
//Console.WriteLine("networkConnectable:{0}. ", Network.networkConnectable.ToString ());
//Console.WriteLine("numberOfBssids:{0}. ", Network.numberOfBssids.ToString ());
//Console.WriteLine("profileName:{0}. ", Network.profileName.ToString ());
//Console.WriteLine("wlanNotConnectableReason:{0}. ", Network.wlanNotConnectableReason.ToString ());
//Console.WriteLine("wlanSignalQuality:{0}. ", Network.wlanSignalQuality.ToString ());
//Console.WriteLine("----------------------------------- ");
// Console.WriteLine (network.ToString ());
}
}
} // EnumSSID
/// <summary>
/// Connect to unencrypted SSID
/// </ summary>
/// <param name = "ssid"> </ param>
public void ConnectToSSID (WIFISSID ssid)
{
// Connects to a known network with WEP security
string profileName = ssid.SSID; // this is also the SSID
string mac = StringToHex (profileName); //
// string key = "";
// 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> New {0} </ name> </ SSID> </ SSIDConfig> <connectionType> ESS </ connectionType > <MSM> <security> <authEncryption> <authentication> open </ authentication> <encryption> none </ 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);
// string profileXml2 = "<? xml version = \" 1.0 \ "?> <WLANProfile xmlns = \" http: //www.microsoft.com/networking/WLAN/profile/v1 \ "> <name> Hacker SSID < / name> <SSIDConfig> <SSID> <hex> 54502D4C494E4B5F506F636B657441505F433844323632 </ hex> <name> TP-LINK_PocketAP_C8D262 </ name> </ SSID> </ SSIDConfig> <connectionType> ESS </ connectionType> <connectionMode> manual </ connectionMode > <MSM> <security> <authEncryption> <authentication> open </ authentication> <encryption> none </ encryption> <useOneX> false </ useOneX> </ authEncryption> </ security> </ MSM> </ WLANProfile> ";
//wlanIface.SetProfile (Wlan.WlanProfileFlags.AllUser, profileXml2, true);
//wlanIface.Connect (Wlan.WlanConnectionMode.Profile