Determining the current Wi-Fi connection speed in C #

I am writing a program that does one thing, it detects the current Wi-Fi connection speed and reports this to the user in real time. The problem I am facing is that it does not seem to be able to figure out the current communication speed, but only the maximum connection speed of the device (300 Mbps). the reason why I am writing this is because I have a problem when, periodically, the connection speed drops sharply (up to 1-2 Mbit / s), and I want to see when this happens. with this code, it will just give me the maximum speed that the adapter supports, and not the current connection speed.

private void update(object state) { System.Net.NetworkInformation.NetworkInterface[] nics = null; nics = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(); long speed = 0; string adapter = ""; foreach (System.Net.NetworkInformation.NetworkInterface net in nics) { if (net.Name.Contains("Wireless") || net.Name.Contains("WiFi") || net.Name.Contains("802.11") || net.Name.Contains("Wi-Fi")) { speed = net.Speed; adapter = net.Name; break; } } string temp; if (speed == 0) { temp = "There is currently no Wi-Fi connection"; } else { temp = "Current Wi-Fi Speed: " + (speed / 1000000) + "Mbps on " + adapter; } if (label1.InvokeRequired) { SetTextCallback d = new SetTextCallback(update); label1.Invoke(d, new object[] { temp }); } else { label1.Text = temp; } } 

to do this, call

 System.Threading.Timer ticker = new System.Threading.Timer(update, label1, 0, 1000); 

in the main method.

+4
source share
2 answers

Given that it took me literally all day to find what it was for a solution, I decided that at least I would show StackOverflow for future reference, what I came across and what did not work for this question.

tl; dr: Highlight the Code section

What i found

Good ol control panel

If you are looking for a really easy way to do this, you can simply open the Contol panel. Depending on which version of Windows you are in (in my case, I am in Windows 8), the path to the page is Control Panel β†’ Network and Internet β†’ Network and Sharing Center, and then you can click on the link next to Connections: which will give you a window similar to the one below. Wi-Fi Info Window
The current line speed is highlighted in red, which in my case is 36.0 Mbps. Although, of course, this may not satisfy your initial question if you intend to integrate some code with the actual value.

Wmi

With a combination of Googling and something else, I thought I found something in the Windows Management Instrumentation .

In short, AFAIK, WMI do not have what we are looking for.

WMI , in a word, is a gigantic database of objects (which can also be requested via SQL), which allows you to request information about a Windows machine, such as a process, disks, etc. In WMI, everything is represented by a class with a series of instances, each of which has a set of properties.
Anyway, WMI Explorer allows you to view all this on your computer.

I (presumably) found two classes on MSDN that might have information about communication speed, but from WMI Explorer, there was nothing useful.

The first class, MSFT_NetAdapter , did not even appear in WMI Explorer on my machine.

The second class, Win32_NetworkAdapter , appeared in WMI Explorer, but the Speed property was still incorrect. The same network adapter showed a value of 168000000 or 168 Mbps, which is incorrect. Although I find it strange because MaxSpeed was already MaxSpeed , but it was empty.

Cross out WMI from the list.

Win32 P / Invoke

Yes, of course, the solution to everything always calls the Win32 unmanaged APIs using P / Invoke magic.

This is the route used to solve the problem .

Fortunately, the problem is IP_ADAPTER_ADDRESSES . If you look at the MSDN page, this is a pretty big structure, but TransmitLinkSpeed is important here.

Calling the GetAdaptersAddresses() function will return the actual structure.

Now, the actual C # P / Invoke code. Fortunately, pinvoke.net already had an interop for this feature, which I added. That was all that was needed.

Code

Finally, here is your code, complemented by the new P / Invoke black magic. I did this as a console application for demo purposes:

Using expressions:

 using System; using System.Threading; 

Code:

 class Program { private static void Main(string[] args) { Timer ticker = new Timer(Update, null, 0, 1000); // Keep the main thread from dying while (true) { Thread.Sleep(1000); } } private static void Update(object state) { ulong speed = 0; string adapter = ""; string[] nameSearches = { "Wireless", "WiFi", "802.11", "Wi-Fi" }; // The enum value of `AF_INET` will select only IPv4 adapters. // You can change this to `AF_INET6` for IPv6 likewise // And `AF_UNSPEC` for either one foreach (IPIntertop.IP_ADAPTER_ADDRESSES net in IPIntertop.GetIPAdapters(IPIntertop.FAMILY.AF_INET)) { bool containsName = false; foreach (string name in nameSearches) { if (net.FriendlyName.Contains(name)) { containsName = true; } } if (!containsName) continue; speed = net.TrasmitLinkSpeed; adapter = net.FriendlyName; break; } string temp; if (speed == 0) { temp = "There is currently no Wi-Fi connection"; } else { temp = string.Format("Current Wi-Fi Speed: {0} Mbps on {1}", (speed / 1000000.0), adapter); } Console.WriteLine(temp); } } 

Then you will look for the actual IPIntertop class that I updated. Since it is quite large, you can find it updated in pinvoke.net or on this PasteBin if something goes down.

Bottom line

On Windows, many APIs that are somewhat broken (WMI) may have some "leaky abstractions" (.Net), or it can be a pain to work with (Win32).

Sigh, that's a lot, and I hope this helps.

+13
source

I am facing the same problem and I need to get the wifi connection speed with Windows, which is currently being negotiated. and thanks to the @Jaxrtech WMI approach that really works. the correct class is the CIM_NetworkAdapter (I am using Windows 7) and query the speed column to get the current speed. while the speed of co-ordinated Wi-Fi is changing, this speed is also changing. I tested it, it was fine.

 select Description , DeviceID, Speed from CIM_NetworkAdapter 

we get:

 D-Link DWA-140 RangeBooster N USB Adapter 17 285000000 
+1
source

Source: https://habr.com/ru/post/1490182/


All Articles