【问题标题】:Connect to a wireless network in C#在 C# 中连接到无线网络
【发布时间】:2017-05-09 19:57:45
【问题描述】:

我正在使用托管 WiFi API 和示例代码:

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 地址。

【问题讨论】:

标签: c# wireless


【解决方案1】:

PhysicalAddress 类应该可以帮助您。在 MSDN 文章中甚至还有一个代码示例,可以从机器上的网络接口中提取 MAC 地址。

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.physicaladdress.aspx

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    有一个很棒的 C# API SimpleWifi,可以如下使用:

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-22
      • 2011-06-01
      • 1970-01-01
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      相关资源
      最近更新 更多