1

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

using Windows.Devices.Radios;

public async void TurnWifiOff()
{
    await Radio.RequestAccessAsync();
    var radios = await Radio.GetRadiosAsync();

    foreach (var radio in radios)
    {
        if (radio.Kind == RadioKind.WiFi)
        {
            await radio.SetStateAsync(RadioState.Off);
        }
    }
}
4

1 回答 1

1

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

using NativeWifi;
using System.Runtime.InteropServices;

{

    WlanClient client = new WlanClient();
    foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
    {
        IntPtr radioStatePtr = new IntPtr(0L);
        try
        {
            Wlan.WlanPhyRadioState radioState = new Wlan.WlanPhyRadioState();
            radioState.dwPhyIndex = 0;
            radioState.dot11HardwareRadioState = Wlan.Dot11RadioState.On;
            radioState.dot11SoftwareRadioState = Wlan.Dot11RadioState.Off; //In this place we set WiFi to be enabled or disabled

            radioStatePtr = Marshal.AllocHGlobal(Marshal.SizeOf(radioState));
            Marshal.StructureToPtr(radioState, radioStatePtr, false);

            Wlan.ThrowIfError(
                Wlan.WlanSetInterface(
                            client.clientHandle,
                            wlanIface.InterfaceGuid,
                            Wlan.WlanIntfOpcode.RadioState,
                            (uint)Marshal.SizeOf(typeof(Wlan.WlanPhyRadioState)),
                            radioStatePtr,
                            IntPtr.Zero));
        }
        finally
        {
            if (radioStatePtr.ToInt64() != 0)
                Marshal.FreeHGlobal(radioStatePtr);
        }
    }
}
于 2019-09-12T10:41:03.513 回答