How do I get a list of available WiFi networks?

Saga 426 Reputation points
2021-04-25T00:28:51.837+00:00

Hi all, I am using Windows 10 with VS2015 and 2019

I am working on a Windows Forms C# App and would like to display a list of available WiFi networks. My objective is to have a list of SSIDs. This is purely informational. I don't plan to have any other networking functionaity associated with this list.

I've done a bunch of research and have come across such concepts as Managed WiFi, Native Wifi, Simple WiFi, and the NetworkInformation.Connectivity namespace, but I have not been able to piece anything together to build a solution for this. I've discarded the namespace becasue another poster here mentioned that this is for UWP apps, not Windows Forms.

Any ideas and suggestions on how to get this list is greatly appreciated. Thanks! Saga

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,887 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,591 Reputation points
    2021-04-26T06:38:07.717+00:00

    You can try to use WiFiAdapter class, it is UWP API, but we can use it by adding two references.

    C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.18362.0\Windows.winmd (you need to change the file type to "All files" in the dialog)

    91094-1.png

    C:\Program Files (x86)\Reference Assembly\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll

    Then use the following code.

    This is not the final code. My desktop does not support WIFI, so I cannot test it. I just hope to provide some ideas.

            public async static Task GetWiFiAdapter()  
            {  
                WiFiAdapter firstAdapter = null;  
                var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector());  
                if (result.Count >= 1)  
                {  
                    firstAdapter = await WiFiAdapter.FromIdAsync(result[0].Id);  
                    await firstAdapter.ScanAsync();  
                    var wifis = firstAdapter.NetworkReport.AvailableNetworks;  
                    foreach (var item in wifis)  
                    {  
                        Console.WriteLine(item.Ssid);  
                    }  
                }  
                else  
                {  
                    Console.WriteLine("No WIFI adapter available");  
                }  
            }  
    

    By the way, many desktop motherboards do not support WIFI, especially the old ones. If you are using these motherboards, I think the code will be useless.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


3 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,411 Reputation points
    2021-04-25T00:50:29.753+00:00

    See if the accepted answer works for you. The library source moved to NuGet here.

    Here is my test app

    using System;  
    using System.Text;  
    using NativeWifi;  
      
    namespace ConsoleApp1  
    {  
        class Program  
        {  
      
            static string GetStringForSSID(Wlan.Dot11Ssid ssid) =>   
                Encoding.ASCII.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);  
      
            static void Main(string[] args)  
            {  
                var client = new WlanClient();  
                foreach (var wlanIface in client.Interfaces)  
                {  
      
                    var networks = wlanIface.GetAvailableNetworkList(0);  
                    foreach (var network in networks)  
                    {  
                        if (network.dot11DefaultCipherAlgorithm == Wlan.Dot11CipherAlgorithm.CCMP)  
                        {  
                            Console.WriteLine("Found WEP network with SSID {0}.", GetStringForSSID(network.dot11Ssid));  
                        }  
                    }  
      
                    Console.WriteLine(new string('-', 40));  
      
      
                    foreach (var profileInfo in wlanIface.GetProfiles())  
                    {  
                        string name = profileInfo.profileName;  
                        /*  
                         * Look at the XML  
                         */  
                        string xml = wlanIface.GetProfileXml(profileInfo.profileName);  
                        Console.WriteLine(name);  
                    }  
      
                }  
      
                Console.ReadLine();  
            }  
        }  
    }  
      
    

    ---

    91052-kpmvp.png

    0 comments No comments

  2. Sam of Simple Samples 5,541 Reputation points
    2021-04-25T01:59:44+00:00

    WMI would be easier. In that Stackoverflow page there is also a suggestion to use WMI. It links to Regression with WiFi scanning using WMI. Developers in that thread had problems using WMI but it was a temporary problem back in the time of Vista that apparently was a bug in an Intel driver. I do not have WiFi with my desktop (so I cannot test it) but the following is the code from MSDN thread. Be sure to add a reference and a using for System.Management.

    String query = "SELECT * FROM MSNDis_80211_BSSIList";
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("root/WMI", query);
    ManagementObjectCollection moc = searcher.Get();
    ManagementObjectCollection.ManagementObjectEnumerator moe = moc.GetEnumerator();
    moe.MoveNext();
    ManagementBaseObject[] objarr = (ManagementBaseObject[])moe.Current.Properties["Ndis80211BSSIList"].Value;
    foreach (ManagementBaseObject obj in objarr)
    {
        uint u_rssi = (uint)obj["Ndis80211Rssi"];
        int rssi = (int)u_rssi;
        // .... then get other properties such as "Ndis80211MacAddress" and "Ndis80211Ssid"
    }
    
    0 comments No comments

  3. Saga 426 Reputation points
    2021-04-25T21:42:09.42+00:00

    Sorry, a bit off topic. I can't find a way to reply to each one of you individually. I don't see a reply button.

    Karen, your code worked. Thank you! However, it only worked on a WiFi enabled device (laptop), not on my desktop (wired Ethernet). I need to display the WiFi SSIDs that are available nearby, independently whether he user runs the app on a desktop (wired) or a laptop (WiFi).

    I had tried with ManagedWiFi, but without success. The information that I fond online directed me to the CodePlex website. I was not able to get that to work. Using NuGet, as you suggested, fixed that issue.

    I appreciate your help, Saga

    SimpleSamples:

    I could not get the code to work. As noted to Karen, I was initially using a desktop; however, your remark about not being able to test on a WiFi enabled device gave me the idea to test on a laptop with WiFi. Still, the code did not work.

    I single step through it and found that after line #3 was executed and I looked into the moc item, Count it noted that Count generated an exception. This exception did not display as usual. To see this I hovered the mouse pointer over moc then hovered it over the triangle. This opened up more information, among this was the error.

    90919-countexception.png

    I did some research and found that tis exception can happen if the WMI service is not running. I checked using the Services app and it was running. I believe there is something that I am missing.

    I appreciate your help. Thanks, Saga


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.