问题
I have tried in 2 different ways
First way: null exception issue
try{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSNdis_80211_ServiceSetIdentifier");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("MSNdis_80211_ServiceSetIdentifier instance");
Console.WriteLine("-----------------------------------");
if (queryObj["Ndis80211SsId"] == null)
{
//Console.WriteLine("Ndis80211SsId: {0}",queryObj["Ndis80211SsId"]);
}
else
{
Byte[] arrNdis80211SsId = (Byte[])
(queryObj["Ndis80211SsId"]);
foreach (Byte arrValue in arrNdis80211SsId)
{
//Console.WriteLine("Ndis80211SsId: {0}", arrValue);
}
}
}
}catch(Exception ex){
}
Second way: I'm gating wi-fi but couldnt get the SSID
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) {
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) {
if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 && ni.OperationalStatus== OperationalStatus.Up ) {
Network = "NETWORK ( N/A )";
Wifi = "Wifi (" + ni.Name + ")";
}
}
}
Could you please someone give me clear idea how to get my connected wifi SSID.
回答1:
// Show SSID and Signal Strength
private void showConnectedId() {
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "netsh.exe";
p.StartInfo.Arguments = "wlan show interfaces";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string s = p.StandardOutput.ReadToEnd();
string s1 = s.Substring(s.IndexOf("SSID"));
s1 = s1.Substring(s1.IndexOf(":"));
s1 = s1.Substring(2, s1.IndexOf("\n")).Trim();
string s2 = s.Substring(s.IndexOf("Signal"));
s2 = s2.Substring(s2.IndexOf(":"));
s2 = s2.Substring(2, s2.IndexOf("\n")).Trim();
labelStatus.Text = "WIFI connected to " + s1 + " " + s2;
p.WaitForExit();
}
回答2:
You can try using below mentioned WMI
classes. Both are defined in cimv2
SELECT * FROM WiFi_AdapterAssociationInfo
SELECT * FROM WiFi_AvailableNetwork
For more details:WIFI Information
回答3:
I found a rather old library dating back to 2014:
Microsoft.WindowsAPICodePack-Core version 1.1.0.2
Although it is not conforming to .NET Standard, this library integrates with my .NET Core 3.0 app, but obviously is not cross-platform.
Sample code:
var networks = NetworkListManager.GetNetworks(NetworkConnectivityLevels.Connected);
foreach (var network in networks) {
sConnected = ((network.IsConnected == true) ? " (connected)" : " (disconnected)");
Console.WriteLine("Network : " + network.Name + " - Category : " + network.Category.ToString() + sConnected);
}
来源:https://stackoverflow.com/questions/39346232/how-to-get-currently-connected-wifi-ssid-in-c-sharp-using-wmi-or-system-net-netw