public void ScanNetworks() { try { _interface.Scan(); } catch (NullReferenceException) { // This shouldn't be throwing an exception, but it can sometimes. // _interface should always be initialized by the time this call is made // Init() is called before the scan loop is started and will not start scanning if the interface is null } }
private void timer1_Tick(object sender, EventArgs e) { try { if (checkBox1.Checked) { listView1.Items.Clear(); if (listBox1.SelectedItem != null) { WlanInterface wlanIface = client.Interfaces.Where(s => s.InterfaceDescription.Equals(listBox1.SelectedItem.ToString())).First(); try { if (wlanIface.InterfaceState == WlanInterfaceState.Connected) { connectedSSID = wlanIface.CurrentConnection.profileName; label1.Text = "Current connection: " + connectedSSID + " " + wlanIface.CurrentConnection.isState; button3.Enabled = true; } else { connectedSSID = string.Empty; label1.Text = "Current connection: " + wlanIface.InterfaceState; button3.Enabled = false; } } catch { connectedSSID = string.Empty; label1.Text = "Current connection: " + wlanIface.InterfaceState; button3.Enabled = false; } wlanIface.Scan(); WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(WlanGetAvailableNetworkFlags.IncludeAllAdhocProfiles); foreach (WlanAvailableNetwork network in networks.GroupBy(x => GetStringForSSID(x.dot11Ssid)).Select(y => y.First()).OrderByDescending(r => r.wlanSignalQuality)) { ListViewItem li = new ListViewItem(); li.Text = GetStringForSSID(network.dot11Ssid); li.SubItems.Add(network.networkConnectable.ToString()); li.SubItems.Add(network.wlanSignalQuality.ToString()); li.SubItems.Add(network.dot11DefaultCipherAlgorithm.ToString()); li.SubItems.Add(network.dot11DefaultAuthAlgorithm.ToString()); li.SubItems.Add(network.securityEnabled.ToString()); try { WlanProfileInfo[] wpi = wlanIface.GetProfiles().Where(s => s.profileName.Equals(li.Text)).ToArray(); li.Tag = (wpi.Length > 0 && wlanIface.InterfaceState != WlanInterfaceState.Connected) ? "true" : "false"; } catch { li.Tag = "false"; } if (selectedSSID == li.Text) { li.Selected = true; if (li.Tag.ToString() == "true") { button2.Enabled = true; } else { button2.Enabled = false; } } if (connectedSSID == li.Text) { li.ForeColor = Color.White; li.BackColor = Color.Green; } else { li.ForeColor = Color.Black; li.BackColor = Color.LightCyan; } listView1.Items.Add(li); } } /*foreach (AccessPoint ap in wifi.GetAccessPoints()) * { * ListViewItem li = new ListViewItem(); * li.Text = ap.Name; * li.SubItems.Add(ap.SignalStrength.ToString()); * li.SubItems.Add(ap.Interface.Channel.ToString()); * li.SubItems.Add(ap.HasProfile.ToString()); * li.SubItems.Add(ap.IsConnected.ToString()); * li.SubItems.Add(ap.IsSecure.ToString()); * if (selectedSSID == li.Text) * { * li.Selected = true; * } * if (connectedSSID == li.Text) * { * li.ForeColor = Color.White; * li.BackColor = Color.Green; * } * else * { * li.ForeColor = Color.Black; * li.BackColor = Color.LightCyan; * } * listView1.Items.Add(li); * }*/ } } catch { } }