从WPF应用程序以编程方式关闭/打开Wi-Fi

时间:2015-12-24 10:48:50

标签: c# wpf visual-studio-2015 windows-10-desktop

我有一个带控件的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);


        }

    }
}

我在第一个答案中经历了以下link,但没有任何帮助。

我需要一些帮助,以便我可以通过点击按钮以编程方式关闭/打开Wi-Fi。

2 个答案:

答案 0 :(得分:1)

您可以通过Native Wifi API更改软件无线电状态(而非硬件无线电状态)来打开/关闭Wi-Fi。使用Managed 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. GetInterfaceNames获取现有Wi-Fi接口名称。
  2. 选择一个界面并按TurnOn启用它。
  3. 通过GetAvailableNetworkProfileNames
  4. 通过界面获取与可用Wi-Fi网络相关联的个人资料名称
  5. 选择个人资料并通过ConnectNetwork
  6. 连接到网络
  7. 使用完网络后,请按TurnOff
  8. 关闭界面

答案 1 :(得分:0)

您可以使用windows universal apps中的设备库。

文档: 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应用程序一起使用,您可以添加

  

&LT; TargetPlatformVersion&gt; 8.0&LT; / TargetPlatformVersion&gt;

之间的.csproj文件
  

&LT; PropertyGroup&gt; ....&lt; /&的PropertyGroup GT;