Get the name of the wifi connected in C#

Asked

Viewed 158 times

0

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

2 answers

1


First you have to create a Wlanclient Object

wlan = new WlanClient();

There you can get a list of SSIDS that the pc is connected with that 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 if there is 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;
    }
}
  • Install the Manegedwifi nuget package

  • Not at all, if Upvote works and Mark as answered :)

  • added a function to show that there is a connection, if Return is Treu, you execute the code if false vc does not execute

  • observation can use any url to serve if there is connection

  • would not be the case to use a Try/catch?

  • If he wants to treat the exception yes, but if only want to check if there is connection can use the function, Some errors are ignored when using Try and even for application

  • Good will have to catch the wifi macAdress, because when it is off does not return macAdress

  • but if the machine has another Ethernet network this may give problem

Show 3 more comments

1

I don’t know how to do it in pure C# but there’s a library ready for it, see here.

I picked up through this reply: https://stackoverflow.com/a/431975/6647038 in English.

Can install through: Console do gerenciador de pacotes

PM> Install-Package managedwifi -Version 1.1.0

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

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 an exception System.ComponentModel.Win32Exception when the wifi is not connected, to resolve this just check whether your wifi is connected or not, using property InterfaceState, follows 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();
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.