C#: Problem with ManagedWiFi

Sep 3, 2013 at 10:53 AM
Edited Sep 4, 2013 at 1:33 AM
Hi every one,

I'm working with ManagedWiFi library. My hope is connecting to special network by SSID. All of step to connecting are in code. I found many sample and create a tiny code that:
           WlanClient.WlanInterface tempIFace = null;
            WlanClient client = new WlanClient();

            foreach ( WlanClient.WlanInterface wlanIface in client.Interfaces )
            {
                // Lists all available networks
                Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList( 0 );
                foreach ( Wlan.WlanAvailableNetwork network in networks )
                {
                    if ( GetStringForSSID( network.dot11Ssid ).Equals( "test" ) )
                    {
                        string profileName = GetStringForSSID( network.dot11Ssid );
                        var macAddress = ConvertToHex( GetStringForSSID( network.dot11Ssid ) );
                        string mac = macAddress;
                        string key = "123456789a";
                        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></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 );
                        tempIFace = wlanIface;
                        tempIFace.SetProfile( Wlan.WlanProfileFlags.AllUser, profileXml, true );
                        tempIFace.Connect( Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, GetStringForSSID( network.dot11Ssid ) );
                        MessageBox.Show( GetStringForSSID( network.dot11Ssid ) );
                        break;
          } 
static string GetStringForSSID( Wlan.Dot11Ssid ssid )
        {
            return Encoding.ASCII.GetString( ssid.SSID, 0, (int)ssid.SSIDLength );
        }

        public string ConvertToHex( string asciiString )
        {
            string hex = "";
            foreach ( char c in asciiString )
            {
                int tmp = c;
                hex += String.Format( "{0:x2}", (uint)System.Convert.ToUInt32( tmp.ToString() ) );
            }
            return hex;
        }
this code run very fine. but cannot connect to this wifi.

Please tell me what is problem? and how to fix it.

P/S: Can ManagedWiFi connect success to ad-hoc network? if can, please tell me it look like abow code? how to do it?
Thanks.