最近有个任务,需要显示Wifi信息(主要是名称和SSID),这个还不简单吗,直接调用同事(我们的系统设置领域专家-旺基 拚忘 - 博客园 (cnblogs.com))写的Uwp 接口不就好了,呵呵 可以获取大部分信息,但是没有Wifi的SSID。查阅资料发现还没有直接能够获取SSID的接口,怎么办??? 和同事思考一翻,拿profile文件做文章。 主要是通过 控制变量法,具体实现如下:
随笔写了一个供参考:
private static string Password(string wiFIName)
{
string netshProfile = $"wlan show profile name = {wiFIName}"; //不带SSID密码
string netshProfilePassword = $"wlan show profile name = {wiFIName} key = clear"; //带SSID密码
string profile = NetshProcess(netshProfile);
string profilePassword = NetshProcess(netshProfilePassword);
var profileArray = FilterProfile(profile);
var profilePasswordArray = FilterProfile(profilePassword);
string password = string.Empty;
int yeirdIndex = Math.Min(profilePasswordArray.Count, profileArray.Count);
for (int i = 0; i < yeirdIndex; i++)
{
if (profilePasswordArray[i] == profileArray[i]) continue;
password = profilePasswordArray[i].Split(new char[] { ':', ':' })[1];
break;
}
return password;
}
private static List<string> FilterProfile(string profile)
{
var profileArray = profile.Split(new char[] { '\r', '\n' }).Reverse().ToList();
for (int i = profileArray.Count - 1; i >= 0; i--)
{
if (string.IsNullOrWhiteSpace(profileArray[i])|| !profileArray[i].Contains(":") && !profileArray[i].Contains(":"))
profileArray.RemoveAt(i);
}
return profileArray;
}
public static string NetshProcess(string args)
{
using (var p = new Process())
{
p.StartInfo = new ProcessStartInfo("netsh", args);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
return output;
}
}