Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过WPF应用程序编程关闭/打开Wi-Fi_C#_Wpf_Visual Studio 2015_Windows 10 Desktop - Fatal编程技术网

C# 通过WPF应用程序编程关闭/打开Wi-Fi

C# 通过WPF应用程序编程关闭/打开Wi-Fi,c#,wpf,visual-studio-2015,windows-10-desktop,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.

我有一个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);


        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfApplication4
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
//string name=“Hello World”;
}
静态无效启用(字符串接口名)
{
System.Diagnostics.ProcessStartInfo psi=
新的System.Diagnostics.ProcessStartInfo(“netsh”,“接口集接口\”+interfaceName+“\”启用”);
System.Diagnostics.Process p=新的System.Diagnostics.Process();
p、 StartInfo=psi;
p、 Start();
}
静态无效禁用(字符串接口名)
{
System.Diagnostics.ProcessStartInfo psi=
新的System.Diagnostics.ProcessStartInfo(“netsh”,“接口集接口\”+interfaceName+“\”禁用”);
System.Diagnostics.Process p=新的System.Diagnostics.Process();
p、 StartInfo=psi;
p、 Start();
}
已选中私有无效复选框(对象发送方、路由目标方)
{
字符串interfaceName=“本地连接”;
禁用(接口名称);
}
}
}
我用第一个答案完成了以下内容,但没有任何帮助


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

您可以从windows universal apps使用设备库

文件:

Microsoft示例:

为了在WPF应用程序中使用此库,您可以添加

8.0

到您的.csproj文件


您可以通过更改软件无线电状态(而不是硬件无线电状态)来打开/关闭Wi-Fi。使用一些项目代码,我编写了一个示例

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);
        }
    }
}
使用系统;
使用System.Linq;
使用System.Runtime.InteropServices;
使用NativeWifi;
公共静态类无线电台
{
公共静态字符串[]GetInterfaceNames()
{
使用(var client=new WlanClient())
{
返回client.Interfaces.Select(x=>x.InterfaceName.ToArray();
}
}
公共静态布尔开启(字符串接口名称)
{
var interfaceGuid=GetInterfaceGuid(interfaceName);
如果(!interfaceGuid.HasValue)
返回false;
返回SetRadioState(interfaceGuid.Value,Wlan.Dot11RadioState.On);
}
公共静态布尔关闭(字符串接口名称)
{
var interfaceGuid=GetInterfaceGuid(interfaceName);
如果(!interfaceGuid.HasValue)
返回false;
返回SetRadioState(interfaceGuid.Value,Wlan.dot1RadioState.Off);
}
私有静态Guid?GetInterfaceGuid(字符串interfaceName)
{
使用(var client=new WlanClient())
{
返回client.Interfaces.FirstOrDefault(x=>x.InterfaceName==InterfaceName)?.InterfaceGuid;
}
}
专用静态bool SetRadioState(Guid接口Guid,Wlan.dot1RadioState)
{
var state=新的Wlan.wlanphyte
{
dwPhyIndex=(int)Wlan.Dot11PhyType.Any,
dot11SoftwareRadioState=无线电状态,
};
var size=Marshal.SizeOf(状态);
变量指针=IntPtr.Zero;
尝试
{
指针=Marshal.AllocHGlobal(大小);
StructureToPtr(状态,指针,false);
var clientHandle=IntPtr.Zero;
尝试
{
不可谈判的争端;
var结果=Wlan.WlanOpenHandle(
Wlan.Wlan\u客户端\u版本\u LONGHORN,
IntPtr.Zero,
在谈判中,
外阴蒂柄);
如果(结果!=0)
返回false;
结果=Wlan.WlanSetInterface(
clientHandle,
接口液体,
Wlan.WlanIntfOpcode.RadioState,
(uint)大小,
指针,
IntPtr.0);
返回值(结果==0);
}
最后
{
Wlan.WlanCloseHandle(
clientHandle,
IntPtr.0);
}
}
最后
{
自由全局元帅(指针);
}
}
公共静态字符串[]GetAvailableNetworkProfileName(字符串接口名)
{
使用(var client=new WlanClient())
{
var wlanInterface=client.Interfaces.FirstOrDefault(x=>x.InterfaceName==InterfaceName);
if(wlanInterface==null)
返回Array.Empty();
返回wlanInterface.GetAvailableNetworkList(Wlan.WlanGetAvailableNetworkFlags.IncludealManualHiddenProfiles)
.选择(x=>x.profileName)
.Where(x=>!string.IsNullOrEmpty(x))
.ToArray();
}
}
公共静态网络(字符串接口名、字符串配置文件名)
{
使用(var client=new WlanClient())
{
var wlanInterface=client.Interfaces.FirstOrDefault(x=>x.InterfaceName==InterfaceName);
如果(WLA接口==nul