コード例 #1
0
        /// <summary>
        /// Wifi 정보를 스캔합니다.
        /// 실제 스캔하는게 아닌 캐시된 정보를 가져옵니다. 만약 실제 스캔중이라면 Sleep 후, 스캔이 완료될 시 정보를 가져옵니다. 따라서 Thread를 권장합니다.
        /// </summary>
        public void Scan()
        {
            lock (lockObj)
            {
                wifiInfos.Clear();

                string cmdResult = Cmd.Run("netsh wlan show networks mode=bssid");

                int loofCount = 0;
                // 만약 스캔중이라면
                while (cmdResult.Contains("BSSID") == false)
                {
                    // 10초를 초과했다면
                    if (loofCount > 10)
                    {
                        MessageBox.Show("Wifi Scan TimeOut. \r\n\r\n", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    Thread.Sleep(1000);
                    cmdResult = Cmd.Run("netsh wlan show networks mode=bssid");
                    ++loofCount;
                }

                string[] lineStr = cmdResult.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < lineStr.Length; ++i)
                {
                    string[] splitStr = lineStr[i].Split(new string[] { " : " }, StringSplitOptions.None);

                    if (splitStr.Length != 2)
                    {
                        continue;
                    }

                    // 전후 공백 삭제.
                    string key   = splitStr[0].Trim();
                    string value = splitStr[1].Trim();

                    // 만약 키가 interface name이면 새로운 딕셔너리 생성.
                    if (key == "Interface name")
                    {
                        wifiInfos.Add(value, new List <Dictionary <string, string> >());
                        continue;
                    }

                    // 만약 키에 SSID가 존재하면 interface 정보안에 새로운 리스트를 생성.
                    if (key.Substring(0, 4) == "SSID")
                    {
                        wifiInfos.Last().Value.Add(new Dictionary <string, string>());
                        key = "SSID";
                    }

                    // 만약 BSSID가 두 개 이상이라면.
                    if (key.Length > 6 && key.Substring(0, 5) == "BSSID" && key.Substring(6, 1) != "1")
                    {
                        Dictionary <string, string> LastDict = wifiInfos.Last().Value.Last();
                        Dictionary <string, string> NewDict  = new Dictionary <string, string>();

                        NewDict.Add("SSID", LastDict["SSID"]);
                        NewDict.Add("Authentication", LastDict["Authentication"]);
                        NewDict.Add("Encryption", LastDict["Encryption"]);

                        wifiInfos.Last().Value.Add(NewDict);

                        continue;
                    }

                    // 키와 값을 넣는다.
                    wifiInfos.Last().Value.Last().Add(key, value);
                }
            }
        }