Connect to WiFi

Oct 20, 2015 at 11:17 AM
I am trying to connect to WiFi network using this code. It list the WiFI details correctly But I am not sure how to connect to a WiFi using this code. Its not able to connect to the WiFI Its showing.
The network connection profile is corrupted
When I click the connect button

I have pasted my code here

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NativeWifi;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

    private struct Message
    {
        public const string ErrorTitle = "Error";
    }

    private struct SSIDParameters
    {
        public byte[] SSID;
        public uint linkQuality;
        public NativeWifi.Wlan.Dot11BssType dot11BssType;
        public string tMac;
        public int rssi;
    }

    List<SSIDParameters> m_paramList = new List<SSIDParameters>();

    public Form1()
    {
        InitializeComponent();
    }

    private void btnRefresh_Click(object sender, EventArgs e)
    {
        SearchSSid();
    }

    private void SearchSSid()
    {
        WlanClient client = new WlanClient();

        foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
        {

            listViewSSID.Items.Clear();
            Wlan.WlanBssEntry[] wlanBssEntries = wlanIface.GetNetworkBssList();

            foreach (Wlan.WlanBssEntry network in wlanBssEntries)
            {
                int rss = network.rssi;
                //     MessageBox.Show(rss.ToString());
                byte[] macAddr = network.dot11Bssid;

                string tMac = "";

                for (int i = 0; i < macAddr.Length; i++)
                {

                    tMac += macAddr[i].ToString("x2").PadLeft(2, '0').ToUpper();

                }

                SSIDParameters _params = new SSIDParameters();

                Console.WriteLine("MAC: {0}.", tMac);

                Console.WriteLine("RSSID:{0}", rss.ToString());
                _params.SSID = network.dot11Ssid.SSID;
                _params.linkQuality = network.linkQuality;
                _params.dot11BssType = network.dot11BssType;
                _params.tMac = tMac;
                _params.rssi = network.rssi;
                m_paramList.Add(_params);
                string networkName = System.Text.ASCIIEncoding.ASCII.GetString(network.dot11Ssid.SSID).ToString();
                ListViewItem item = new ListViewItem(networkName);
                item.SubItems.Add(network.linkQuality + "%");
                listViewSSID.Items.Add(item);
            }
            Console.ReadLine();
        }
    }

    private void btnConnect_Click(object sender, EventArgs e)
    {       
        string profileName = "";
        string mac = "";
        string key = txtSecurityKey.Text;
        string selectedssid = listViewSSID.SelectedItems[0].Text;
        foreach (SSIDParameters _parameter in m_paramList)
        {
            if (System.Text.ASCIIEncoding.ASCII.GetString(_parameter.SSID).ToString() == selectedssid)
            {
                profileName = selectedssid.Replace("\0", ""); ;
                mac = _parameter.tMac;
            }
        }

        WlanClient client = new WlanClient();

        foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
        {
            try
            {
                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>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><MSM><security><authEncryption><authentication>open</authentication><encryption>WEP</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);
                wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
                wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
}