2

我有一个 WPF 应用程序,它有一个 control(checkbox /toggle switch) 。我想使用这些按钮打开/关闭 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。

4

2 回答 2

2

您可以通过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,它应该需要管理员权限,但它不在我的环境中。


补充

我在这个类中添加了另外两个方法。所以顺序将是这样的。

  1. 通过 获取现有的 Wi-Fi 接口名称GetInterfaceNames
  2. 选择一个接口并按 将其打开TurnOn
  3. 通过 接口获取与可用 Wi-Fi 网络关联的配置文件名称GetAvailableNetworkProfileNames
  4. 选择一个配置文件并通过 连接到网络ConnectNetwork
  5. 使用完网络后,按 关闭接口TurnOff
于 2015-12-25T12:06:25.730 回答
0

您可以使用 Windows 通用应用程序中的设备库。

文档: https ://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.wifi.aspx

微软示例: https ://github.com/Microsoft/Windows-universal-samples/tree/master/Samples

为了将此库与 WPF 应用程序一起使用,您可以添加

< TargetPlatformVersion > 8.0</ TargetPlatformVersion >

到您的 .csproj 文件之间

<PropertyGroup>.... </PropertyGroup>

于 2015-12-24T15:33:01.607 回答