Some time ago I was asked to write some code to
- detect if a user had saved a connection to an Open/Public Wifi point, and
- delete it if it exists.
The code had to work on Windows 7, so using the Wi-Fi Direct feature wasn’t available to me.
I could do this using the Native WiFi API, except that doing that from .Net isn’t easy.
Everything worked well except …
The code would occasionally fail. Much reading of documentation, and lots of head scratching happened. This was the answer:
“All wireless LAN functions require an interface GUID for the wireless interface when performing profile operations. When a wireless interface is removed, its state is cleared from Wireless LAN Service (WLANSVC) and no profile operations are possible.”
– WlanSetProfile function
What that means in practice:
- if the Wireless LAN Service is not running, then there is no WiFi running
- if the user has disabled the WiFi Adapter, then there is no WiFi to query.
So if 1 or 2 occurred, the code I wrote would fail.
The code to fix that was as simple as this:
Private Sub DeleteOpenWifi()
If wifi.Interfaces.Length = 0 Then