无法在Vista / 7中获取已连接网络的配置文件

时间:2011-07-05 06:02:41

标签: c# windows-7 windows-vista adhoc wlanapi

我有一段使用Managed Native WiFi实现的代码。 它用于获取连接网络的配置文件详细信息,以便在屏幕上显示。

 
               // Start the wireless configuration service if it is stopped
                startservice();
                WlanClient client = new WlanClient();
                bHasWiFi = false;
                string strConnectionStatus = Constants.BlankString;

                // Enumerate all interfaces
                foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
                {
                    bHasWiFi = true;
                    strConnectionStatus = wlanIface.InterfaceState.ToString();

                    // Check whether disconnected
                    if (strConnectionStatus == Constants.Disconnected)
                    {
                        IsConnected = false;
                        continue; //iterate to next interface if any
                    }
                    else
                    {
                        string strProfileName = wlanIface.CurrentConnection.profileName.ToString();
                        ErrorLog.WriteLogMsg("Connected Profile  : " + strProfileName);
                        strDefaultNetwork = wlanIface.CurrentConnection.wlanAssociationAttributes.dot11BssType.ToString();
                        ErrorLog.WriteLogMsg("Connected Network Type  : " + strDefaultNetwork);
                        if (strProfileName.Length != 0)
                        {
                            // Obtain Profile XML
                            string strXmlProfile = wlanIface.GetProfileXml(strProfileName);

                            // Read channel information if OS not Windows XP
                            if(strCurOS != Constants.WindowsXP)
                                intChannel = wlanIface.Channel;

                            // Extract profile information
                            GetNetworkProfileXML(ref IsConnected, ref strDefaultAuth, ref strDefaultSSID, ref strDefaultEnc, ref strDefaultKey, wlanIface, ref strXmlProfile);

                            // Process and store the profile data
                            GetDatfromProfiles(strDefaultNetwork, strDefaultAuth, strDefaultSSID, strDefaultEnc, strDefaultKey, strCurOS, intChannel);
                        }
                        else
                        {
                            ErrorLog.WriteLogMsg("Blank profile name");
                            IsConnected = false; // Set error flag
                        }
                        break;
                    }
                }

                // Error cases
                if (!IsConnected || !bHasWiFi)
                {
                    if (!bHasWiFi)
                        throw new Exception("Unable to enumerate the wireless interfaces");
                    else if (!IsConnected)
                        throw new Exception("WiFi is not configured or is disconnected");
                }
            }

每当在Vista / Windows 7中执行此代码并且在没有保存配置文件的情况下连接网络时,方法GetProfileXMl将抛出错误

  

01:18:12方法:ThrowIfError

     

01:18:12记录消息:找不到元素

     

01:18:12 Stack Trace:在NativeWifi.Wlan.ThrowIfError(Int32 win32ErrorCode)      在NativeWifi.WlanClient.WlanInterface.GetProfileXml(String profileName)

据观察,在Vista和Windows 7中,如果用户未选择“自动连接”,则在连接基础架构期间不会保存配置文件。此外,永远不会保存adhoc配置文件,因为在连接期间不会向用户提供此选项。

有人知道如何读取连接配置文件的XML /详细信息,即使它没有保存吗? 提前谢谢。

1 个答案:

答案 0 :(得分:1)

如果您在启动连接之前注册通知(请参阅here),您应该能够在收到wlan_notification_acm_connection_complete通知时检查WLAN_CONNECTION_NOTIFICATION_DATA structure的strProfileXml字段。