C# 使用本机wifi配置文件连接到特定wifi错误

C# 使用本机wifi配置文件连接到特定wifi错误,c#,.net,wireless,wifi,C#,.net,Wireless,Wifi,我正在使用NativeWifi API连接到特定的无线网络,但我似乎得到的配置文件已损坏错误。我尝试过在不覆盖配置文件的情况下连接,但也不起作用。我知道我的ssid和密码是正确的。我认为我正在阅读的mac电脑是不正确的,但在这一点上,我已经尝试了所有我能想到的mac电脑。如果有人能给我指出正确的方向,我将不胜感激 大部分代码都是我在网上找到的,但我找不到为什么这个可行的解决方案对我不起作用。请记住,我正在尝试建立的连接是到我的手机hotstop 仅供参考:我已经尝试了所有重复的问题建议,但解决方

我正在使用NativeWifi API连接到特定的无线网络,但我似乎得到的配置文件已损坏错误。我尝试过在不覆盖配置文件的情况下连接,但也不起作用。我知道我的ssid和密码是正确的。我认为我正在阅读的mac电脑是不正确的,但在这一点上,我已经尝试了所有我能想到的mac电脑。如果有人能给我指出正确的方向,我将不胜感激

大部分代码都是我在网上找到的,但我找不到为什么这个可行的解决方案对我不起作用。请记住,我正在尝试建立的连接是到我的手机hotstop

仅供参考:我已经尝试了所有重复的问题建议,但解决方案对我不起作用

WlanClient client = new WlanClient();

            foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
            {
                if (wlanIface.CurrentConnection.profileName == "AndroidAP")
                {
                    if (wlanIface.CurrentConnection.isState == Wlan.WlanInterfaceState.Connected)
                    {
                        Console.WriteLine("Already connected to AndroidAP");
                        Console.ReadKey();
                    }
                }
                // Retrieves XML configurations of existing profiles.
                // This can assist you in constructing your own XML configuration
                // (that is, it will give you an example to follow).
                foreach (Wlan.WlanProfileInfo profileInfo in wlanIface.GetProfiles())
                {
                    string name = profileInfo.profileName; // this is typically the network's SSID                    
                    string xml = wlanIface.GetProfileXml(profileInfo.profileName);
                }

                string mac = String.Empty;
                foreach (Wlan.WlanBssEntry network2 in wlanIface.GetNetworkBssList())
                {
                    byte[] macAddr = network2.dot11Bssid;
                    if (GetStringForSSID(network2.dot11Ssid) == "AndroidAP")
                    {                        
                        for (int i = 0; i < macAddr.Length - 1; i++)
                        {
                            mac += macAddr[i].ToString("x2").PadLeft(2, '0').ToUpper();
                        }

                        // Connects to a known network with WEP security
                        string profileName = "AndroidAP"; // this is also the SSID                        
                        string key = "password";

                        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);
                    }                    
                }                
            }
WlanClient client=new WlanClient();
foreach(客户端接口中的WlanClient.WlanInterface-wlanface)
{
如果(wlanIface.CurrentConnection.profileName==“AndroidAP”)
{
if(wlanIface.CurrentConnection.isState==Wlan.WlanInterfaceState.Connected)
{
Console.WriteLine(“已连接到AndroidAP”);
Console.ReadKey();
}
}
//检索现有配置文件的XML配置。
//这可以帮助您构建自己的XML配置
//(也就是说,它将为您提供一个可以遵循的示例)。
foreach(wlanIface.GetProfiles()中的Wlan.WlanProfileInfo)
{
string name=profileInfo.profileName;//这通常是网络的SSID
字符串xml=wlanIface.GetProfileXml(profileInfo.profileName);
}
string mac=string.Empty;
foreach(wlanIface.GetNetworkBssList()中的Wlan.WlanBssEntry network2)
{
字节[]macAddr=network2.dot11Bssid;
if(GetStringForSSID(network2.dot11Ssid)=“AndroidAP”)
{                        
对于(int i=0;i
根据 您不应在
元素中提供mac地址,而应提供
的十六进制表示形式

另请注意(从上面的链接):

尽管hex和name元素是可选的,但至少有一个hex或name元素必须显示为SSID元素的子元素