操作步骤很简单:
关于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.创建C#工程文件,并添加对ManagedWifi.dll的引用。
3.编写代码,引用“Native Wifi API”。
关键代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
using
using
using
using
namespace
{
class
{
public
new
public
{
ssids.Clear();
}
static
GetStringForSSID(Wlan.Dot11Ssid ssid)
{
return
int
)ssid.SSIDLength);
}
/// <summary>
/// 枚举所有无线设备接收到的SSID
/// </summary>
public
ScanSSID()
{
WlanClient client =
new
foreach
in
{
// Lists all networks with WEP security
Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
foreach
in
{
WIFISSID targetSSID =
new
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>
/// 连接到未加密的SSID
/// </summary>
/// <param name="ssid"></param>
public
ConnectToSSID(WIFISSID ssid)
{
// Connects to a known network with WEP security
string
// this is also the SSID
string
//
//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, Wlan.Dot11BssType.Any, profileName );
string
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>manual</connectionMode><MSM><security><authEncryption><authentication>open</authentication><encryption>none</encryption><useOneX>false</useOneX></authEncryption></security></MSM></WLANProfile>"
, profileName, mac);
ssid.wlanInterface.SetProfile(Wlan.WlanProfileFlags.AllUser, myProfileXML,
true
);
ssid.wlanInterface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
//Console.ReadKey();
}
/// <summary>
/// 字符串转Hex
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public
string
StringToHex(
string
{
StringBuilder sb =
new
byte
[] byStr = System.Text.Encoding.Default.GetBytes(str);
//默认是System.Text.Encoding.Default.GetBytes(str)
for
int
{
sb.Append(Convert.ToString(byStr[i], 16));
}
return
}
}
class
{
public
SSID =
"NONE"
;
public
dot11DefaultAuthAlgorithm =
""
;
public
dot11DefaultCipherAlgorithm =
""
;
public
networkConnectable =
true
;
public
wlanNotConnectableReason =
""
;
public
wlanSignalQuality = 0;
public
null
;
}
}
|
4.示例程序允许枚举当前网卡接收到的所有无线SSID,并支持接入开放认证(无密码)的无线热点。
5.其它
Wifi XML配置文件请参考微软文档
http://www.microsoft.com/networking/WLAN/profile/v1