다음을 통해 공유


C#: Managed Wifi API

1. Download the Managed Wifi API:

http://managedwifi.codeplex.com/

About Managed Wifi API: This project is a .NET class library allow 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.To code it:

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);
        }
        /// Enumerate the SSIDs received by all wireless devices
        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.dot11DefaultAuthAlgorithm = network.dot11DefaultAuthAlgorithm.ToString();
                    targetSSID.dot11DefaultCipherAlgorithm = network.dot11DefaultCipherAlgorithm.ToString();
                    ssids.Add(targetSSID);
                }
            }
        } // EnumSSID
        /// Connect to an unencrypted SSID
        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 myProfileXML = 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);
        }
        /// String to Hex
        public static   string StringToHex(string str)
        {
            StringBuilder sb = new  StringBuilder();
            byte[] byStr = System.Text.Encoding.Default.GetBytes(str);
            for (int i = 0; i < byStr.Length; i++)
            {
                sb.Append(Convert.ToString(byStr[i], 16));
            }
            return (sb.ToString().ToUpper());
        }
    }
    class WIFISSID
    {
        public string   SSID = "NONE";
        public string   dot11DefaultAuthAlgorithm = "";
        public string   dot11DefaultCipherAlgorithm = "";
        public bool  networkConnectable = true;
        public string  wlanNotConnectableReason = "";
        public int  wlanSignalQuality = 0;
        public WlanClient.WlanInterface wlanInterface = null;
    }
}

4. The sample program allows enumeration of all wireless SSIDs received by the current network card and supports wireless hotspots that access open authentication (no password).