我有一个类似的问题,我需要获取当前连接的 Wifi 网络的 SSID,但由于其复杂性,我不想为 API 创建一个包装器,所以我想为什么不使用 netsh
ProcessStartInfo info = new ProcessStartInfo("netsh", "wlan show interfaces");
info.WorkingDirectory = @"%WINDIR%\system32";
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
info.CreateNoWindow = true;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = info;
proc.Start();
然后您可以从 proc.StandardOutput.ReadToEnd(); 检索输出 从字符串中解析出你想要的内容:
"\r\n There is 1 interface on the system: \r\n\r\n
Name : Wireless Network Connection\r\n
Description : Atheros AR9285 Wireless Network Adapter\r\n
GUID : xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\r\n
Physical address : xx:xx:xx:xx:xx:xx\r\n
State : connected\r\n
SSID : Dynex2\r\n
BSSID : xx:xx:xx:xx:xx:xx\r\n
Network type : Infrastructure\r\n
Radio type : 802.11g\r\n
Authentication : WPA2-Personal\r\n
Cipher : CCMP\r\n
Connection mode : Auto Connect\r\n
Channel : 1\r\n
Receive rate (Mbps) : 54\r\n
Transmit rate (Mbps) : 54\r\n
Signal : 100% \r\n
Profile : Dynex2 \r\n\r\n
Hosted network status : Not available\r\n\r\n"
解析字符串比为 API 编写包装器要容易得多希望这会有所帮助