2011-06-11 145 views
5

我使用的是托管的WiFi API和示例代码:连接到C#无线网络

string profileName = "Cheesecake"; // this is also the SSID 
string mac = "52544131303235572D454137443638"; 
string key = "hello"; 
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); 

我更新为使用我的SSID和关键的代码,但我不知道我应该如何获取MAC地址。

+0

的可能重复的[得到的MAC地址可无线网络使用WlanApi C#连接](HTTP://计算器。 COM /问题/ 4364475/GET-MAC地址换Wirless的网络到使用连接 - - wlanapi-C) – 2011-06-11 21:19:28

回答

0

有一个伟大的API SimpleWifi为C#和可以用作下:

string ssid = "abc wifi network"; 
      AccessPoint selectedAP = null; 
      bool isApFound = false; 

      foreach (AccessPoint ap in accessPoints) 
      { 
       if (ap.Name.Equals(ssid, StringComparison.InvariantCultureIgnoreCase)) 
       { 
        selectedAP = ap; 
        isApFound = true; 
        break; 
       }      
      } 

      if(!isApFound) 
      { 

       MessageBox.Show("SSID: " + ssid + " not found in range."); 
       return; 

      } 

      // Auth 
      AuthRequest authRequest = new AuthRequest(selectedAP); 
      bool overwrite = true; 

      if (authRequest.IsPasswordRequired) 
      { 
       if (selectedAP.HasProfile) 
       // If there already is a stored profile for the network, we can either use it or overwrite it with a new password. 
       { 
        var confirmResult = MessageBox.Show("A network profile already exist, do you want to use it ?", "Confirm Yes ?", MessageBoxButtons.YesNo); 
        if (confirmResult == DialogResult.Yes) 
        { 
         overwrite = false; 
        } 
       } 

       if (overwrite) 
       { 
        if (authRequest.IsUsernameRequired) 
        { 

         authRequest.Username = Microsoft.VisualBasic.Interaction.InputBox("Please enter Wifi username", "Wifi Username", "", -1, -1); 

        } 

        authRequest.Password = PasswordPrompt(selectedAP); 

        if (authRequest.IsDomainSupported) 
        { 
         authRequest.Domain = Microsoft.VisualBasic.Interaction.InputBox("Please enter Wifi domain", "Wifi Domain", "", -1, -1); 
        } 
       } 
      } 

      selectedAP.ConnectAsync(authRequest, overwrite, OnConnectedComplete);