question

Saga-3468 avatar image
0 Votes"
Saga-3468 asked Saga-3468 commented

How do I get a list of available WiFi networks?

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

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered Saga-3468 commented

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.


1.png (15.3 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks Timon Yang. Your detailed reply and effort are appreciated; however, if you note in my original post, I mention that I am working on a Windows Forms App, and if I understand correctly, the UWP technique that you present won;t work. If I am mistaken, please let me know and I'll test your code on a WiFi enabled device.

Also of mention, the technique to get the SSIDs must work regardless of whether the computer has WiFi or not, so this limits my options greatly. See my plan B in my reply to SimpleSamples. Again, thank you for your help. Saga

0 Votes 0 ·

Since Timon Yang says but, the implication is that it works for Windows Forms too. You can wait for Timon Yang to reply or you can try it to see if it works.

0 Votes 0 ·

@Saga-3468
We can call the UWP API from the Windows Forms application.
Please see the following Windows Developer Blog:
Calling Windows 10 APIs From a Desktop Application
Calling Windows 10 APIs From a Desktop Application just got easier

0 Votes 0 ·

OK, I see, thanks.

I am interested in this technique of accessing Windows API from a desktop application.

Saga

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

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



kpmvp.png (2.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SimpleSamples avatar image
0 Votes"
SimpleSamples answered SimpleSamples edited

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"
 }




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Saga-3468 avatar image
0 Votes"
Saga-3468 answered Saga-3468 commented

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



countexception.png (381.3 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Saga-3468

Unfortunately I only have laptops to test with at home and all neighbors are the same. If I was allowed to go to work (our governor has forbidden going into the workplace and have been working from home for the past 13 months) I could test it but unable too.



90988-kpmvp.png


0 Votes 0 ·
kpmvp.png (2.0 KiB)

Thanks again Karen! No worries. I've been home since March 17, 2020 and today is my first day back at the office :-).

I am working on an alternative way to get the SSIDs in case I can't get them using one of the techniques mentioned here. Regards, Saga

0 Votes 0 ·

For replying, there is a Comment link at the bottom of posts. You can reply to posts that way. I am replying to your post that way.

WMI Explorer is useful for exploring what is available using WMI. WMI takes a little time to learn but there is very much available using WMI.

It is probably not possible to get SSIDs using a device that does not have WiFi. Maybe if you were to ask the router, but that would be complicated.

And that code should have at least one try/catch block.


0 Votes 0 ·

Thanks, I am now seeing 'Reply" links at the bottom of the messages. Go figure.

I understand what you are saying. Luckily, I have a plan B. The app I am working on is a set up app for a Wifi enabled IoT device that will be connected to the PC via the USB port (only for the setup process), emulating a serial port. The device can get a list of SSIDs, so I plan to query the device for this list and get it through the (USB) serial port. Your help is appreciated. Saga

0 Votes 0 ·