Get SSID of the wireless network I am connected to with C# .Net on Windows Vista

Solution 1:

I resolved using the library. It resulted to be quite easy to work with the classes provided:

First I had to create a WlanClient object

wlan = new WlanClient();

And then I can get the list of the SSIDs the PC is connected to 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)));
}

Solution 2:

We were using the managed wifi library, but it throws exceptions if the network is disconnected during a query.

Try:

var process = new Process
{
    StartInfo =
    {
    FileName = "netsh.exe",
    Arguments = "wlan show interfaces",
    UseShellExecute = false,
    RedirectStandardOutput = true,
    CreateNoWindow = true
    }
};
process.Start();

var output = process.StandardOutput.ReadToEnd();
var line = output.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(l => l.Contains("SSID") && !l.Contains("BSSID"));
if (line == null)
{
    return string.Empty;
}
var ssid = line.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries)[1].TrimStart();
return ssid;