[英]C# - How to programmatically turn WiFi off Windows 8 10
我正在编写一个禁用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);
}
}
}
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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.