Get the name of the connected wifi in C #

0

Obs: Não tem muita explicação, apenas obter o nome do wifi conectado.

    
asked by anonymous 01.03.2018 / 16:30

2 answers

1

First you have to create a WlanClient object

wlan = new WlanClient();

Here you can get a list of SSIDS that the pc is connected with this code:

Collection<String> connectedSsids = new Collection<string>();

        foreach (WlanClient.WlanInterface wlanInterface in wlan.Interfaces)
        {
            Wlan.Dot11Ssid ssid = wlanInterface.CurrentConnection.wlanAssociationAttributes.dot11Ssid;
            connectedSsids.Add(new String(Encoding.ASCII.GetChars(ssid.SSID,0, (int)ssid.SSIDLength)));
        }

To check for a connection you can use the code below

System.Net.WebClient

public static bool CheckForInternetConnection()
{
    try
    {
        using (var client = new WebClient())
        {
            using (client.OpenRead("http://clients3.google.com/generate_204"))
            {
                return true;
            }
        }
    }
    catch
    {
        return false;
    }
}
    
01.03.2018 / 17:11
1

I do not know how to do this in pure C #, but have a library ready for it, see here .

I picked up this answer: link in English.

You can install through: Console do gerenciador de pacotes

PM> Install-Package managedwifi -Version 1.1.0

See how to get the name of the wifi: (Note: this only works with wifi on)

private string GetWifiName()
{
    WlanClient wlanClient = new WlanClient();

    List<String> list = new List<String>();

    foreach (WlanInterface wlanInterface in wlanClient.Interfaces)
    {
        Wlan.Dot11Ssid ssid = wlanInterface.CurrentConnection.wlanAssociationAttributes.dot11Ssid;
        list.Add(new String(Encoding.ASCII.GetChars(ssid.SSID, 0, (int)ssid.SSIDLength)));
    }

    return list.FirstOrDefault();
}

The above code generates a System.ComponentModel.Win32Exception exception when wifi is not connected, to solve this, just check if your wifi is connected or not, using the InterfaceState property, follow the final example:

private string GetWifiName()
{
    WlanClient wlanClient = new WlanClient();

    List<String> list = new List<String>();

    foreach (WlanInterface wlanInterface in wlanClient.Interfaces)
    {
        if (wlanInterface.InterfaceState == Wlan.WlanInterfaceState.Connected)
        {
            Wlan.Dot11Ssid ssid = wlanInterface.CurrentConnection.wlanAssociationAttributes.dot11Ssid;
            list.Add(new String(Encoding.ASCII.GetChars(ssid.SSID, 0, (int)ssid.SSIDLength)));
        }
    }

    return list.FirstOrDefault();
} 
    
01.03.2018 / 17:11