C# -如何以编程方式关闭WiFi Windows 8 10

2ekbmq32  于 2023-11-21  发布在  Windows
关注(0)|答案(1)|浏览(227)

我正在编写一个应用程序禁用WiFi.这不能通过禁用网络适配器使用WiFi来完成,或调用netsh等.对于Windows 10我有以下代码工作.如何为Windows 8做同样的事情?

  1. using Windows.Devices.Radios;
  2. public async void TurnWifiOff()
  3. {
  4. await Radio.RequestAccessAsync();
  5. var radios = await Radio.GetRadiosAsync();
  6. foreach (var radio in radios)
  7. {
  8. if (radio.Kind == RadioKind.WiFi)
  9. {
  10. await radio.SetStateAsync(RadioState.Off);
  11. }
  12. }
  13. }

字符串

vojdkbi0

vojdkbi01#

Mike Petrichenko,谢谢你的提示。我已经找到了一个基于“wlanapi.dll”的解决方案,该解决方案是由Codebooks提供的:https://archive.codeplex.com/?p=managedwifi在将两个文件“Interop. cs”和“WlanApi.cs”添加到项目中之后,以下代码完成了这项工作

  1. using NativeWifi;
  2. using System.Runtime.InteropServices;
  3. {
  4. WlanClient client = new WlanClient();
  5. foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
  6. {
  7. IntPtr radioStatePtr = new IntPtr(0L);
  8. try
  9. {
  10. Wlan.WlanPhyRadioState radioState = new Wlan.WlanPhyRadioState();
  11. radioState.dwPhyIndex = 0;
  12. radioState.dot11HardwareRadioState = Wlan.Dot11RadioState.On;
  13. radioState.dot11SoftwareRadioState = Wlan.Dot11RadioState.Off; //In this place we set WiFi to be enabled or disabled
  14. radioStatePtr = Marshal.AllocHGlobal(Marshal.SizeOf(radioState));
  15. Marshal.StructureToPtr(radioState, radioStatePtr, false);
  16. Wlan.ThrowIfError(
  17. Wlan.WlanSetInterface(
  18. client.clientHandle,
  19. wlanIface.InterfaceGuid,
  20. Wlan.WlanIntfOpcode.RadioState,
  21. (uint)Marshal.SizeOf(typeof(Wlan.WlanPhyRadioState)),
  22. radioStatePtr,
  23. IntPtr.Zero));
  24. }
  25. finally
  26. {
  27. if (radioStatePtr.ToInt64() != 0)
  28. Marshal.FreeHGlobal(radioStatePtr);
  29. }
  30. }
  31. }

字符串

展开查看全部

相关问题