通过WPF应用程序以程序方式关闭/打开Wi-Fi
本文关键字:打开 Wi-Fi 方式关 程序 WPF 应用程序 通过 | 更新日期: 2023-09-27 18:28:01
我有一个WPF应用程序,它有一个控件(复选框/切换开关)。我想用那些按钮打开/关闭Wi-Fi。我已经尝试了以下代码,但它似乎对没有帮助
我正在使用Windows 10和Visual Studio 2015
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// string name = "Hello World";
}
static void Enable(string interfaceName)
{
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo("netsh", "interface set interface '"" + interfaceName + "'" enable");
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = psi;
p.Start();
}
static void Disable(string interfaceName)
{
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo("netsh", "interface set interface '"" + interfaceName + "'" disable");
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = psi;
p.Start();
}
private void checkBox_Checked(object sender, RoutedEventArgs e)
{
string interfaceName = "Local Area Connection";
Disable(interfaceName);
}
}
}
我用第一个答案浏览了下面的链接,但没有任何帮助。
我需要一些帮助,这样我就可以通过程序点击按钮关闭/打开Wi-Fi。
您可以通过Native Wifi API更改软件无线电状态(而不是硬件无线电状态)来打开/关闭Wi-Fi。使用管理Wifi API项目的一些代码,我编写了一个示例。
using System;
using System.Linq;
using System.Runtime.InteropServices;
using NativeWifi;
public static class WlanRadio
{
public static string[] GetInterfaceNames()
{
using (var client = new WlanClient())
{
return client.Interfaces.Select(x => x.InterfaceName).ToArray();
}
}
public static bool TurnOn(string interfaceName)
{
var interfaceGuid = GetInterfaceGuid(interfaceName);
if (!interfaceGuid.HasValue)
return false;
return SetRadioState(interfaceGuid.Value, Wlan.Dot11RadioState.On);
}
public static bool TurnOff(string interfaceName)
{
var interfaceGuid = GetInterfaceGuid(interfaceName);
if (!interfaceGuid.HasValue)
return false;
return SetRadioState(interfaceGuid.Value, Wlan.Dot11RadioState.Off);
}
private static Guid? GetInterfaceGuid(string interfaceName)
{
using (var client = new WlanClient())
{
return client.Interfaces.FirstOrDefault(x => x.InterfaceName == interfaceName)?.InterfaceGuid;
}
}
private static bool SetRadioState(Guid interfaceGuid, Wlan.Dot11RadioState radioState)
{
var state = new Wlan.WlanPhyRadioState
{
dwPhyIndex = (int)Wlan.Dot11PhyType.Any,
dot11SoftwareRadioState = radioState,
};
var size = Marshal.SizeOf(state);
var pointer = IntPtr.Zero;
try
{
pointer = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(state, pointer, false);
var clientHandle = IntPtr.Zero;
try
{
uint negotiatedVersion;
var result = Wlan.WlanOpenHandle(
Wlan.WLAN_CLIENT_VERSION_LONGHORN,
IntPtr.Zero,
out negotiatedVersion,
out clientHandle);
if (result != 0)
return false;
result = Wlan.WlanSetInterface(
clientHandle,
interfaceGuid,
Wlan.WlanIntfOpcode.RadioState,
(uint)size,
pointer,
IntPtr.Zero);
return (result == 0);
}
finally
{
Wlan.WlanCloseHandle(
clientHandle,
IntPtr.Zero);
}
}
finally
{
Marshal.FreeHGlobal(pointer);
}
}
public static string[] GetAvailableNetworkProfileNames(string interfaceName)
{
using (var client = new WlanClient())
{
var wlanInterface = client.Interfaces.FirstOrDefault(x => x.InterfaceName == interfaceName);
if (wlanInterface == null)
return Array.Empty<string>();
return wlanInterface.GetAvailableNetworkList(Wlan.WlanGetAvailableNetworkFlags.IncludeAllManualHiddenProfiles)
.Select(x => x.profileName)
.Where(x => !string.IsNullOrEmpty(x))
.ToArray();
}
}
public static void ConnectNetwork(string interfaceName, string profileName)
{
using (var client = new WlanClient())
{
var wlanInterface = client.Interfaces.FirstOrDefault(x => x.InterfaceName == interfaceName);
if (wlanInterface == null)
return;
wlanInterface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
}
}
}
通过GetInterfaceNames检查可用的接口名称,然后使用其中一个名称调用TurnOn/TurnOff。根据MSDN的说法,它应该需要管理员特权,但在我的环境中没有。
补充
我又向这个类添加了两个方法。所以序列会是这样的。
- 通过
GetInterfaceNames
获取现有的Wi-Fi接口名称 - 选择一个接口,然后通过
TurnOn
将其打开 - 通过
GetAvailableNetworkProfileNames
接口获取与可用Wi-Fi网络关联的配置文件名称 - 选择配置文件并通过
ConnectNetwork
连接到网络 - 使用完网络后,通过
TurnOff
关闭接口
您可以使用windows通用应用程序中的设备库。
文件:https://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.wifi.aspx
Microsoft示例:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples
为了将此库与WPF应用程序一起使用,您可以添加
<TargetPlatformVersion>8.0</目标平台版本>
到之间的.csproj文件
<PropertyGroup><PropertyGroup>