You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.NET Framework的WPF项目中访问UWP的NetworkOperatorTetheringManager类

在.NET Framework WPF应用中控制Windows 10移动热点的方案

下面提供几种可行的实现方式,替代无法直接引用的UWP NetworkOperatorTetheringManager 类:

方法一:通过Windows Runtime组件封装UWP API

这是最直接且官方推荐的方式,通过创建中间组件桥接WPF和UWP API:

  1. 创建Windows Runtime Component项目
    在解决方案中添加一个Windows Runtime Component(.NET)项目,在其中封装移动热点控制逻辑:

    using Windows.Networking.NetworkOperators;
    
    namespace TetheringControl
    {
        public sealed class HotspotManager
        {
            // 启用移动热点
            public async System.Threading.Tasks.Task<bool> EnableHotspot()
            {
                var tetheringManager = NetworkOperatorTetheringManager.CreateFromConnectionProfile(null);
                var operationResult = await tetheringManager.StartTetheringAsync();
                return operationResult.Status == TetheringOperationStatus.Success;
            }
    
            // 禁用移动热点
            public async System.Threading.Tasks.Task<bool> DisableHotspot()
            {
                var tetheringManager = NetworkOperatorTetheringManager.CreateFromConnectionProfile(null);
                var operationResult = await tetheringManager.StopTetheringAsync();
                return operationResult.Status == TetheringOperationStatus.Success;
            }
        }
    }
    
  2. 在WPF项目中引用组件
    将上述Runtime Component项目添加为WPF项目的引用,然后在WPF代码中异步调用:

    private async void ToggleHotspotButton_Click(object sender, RoutedEventArgs e)
    {
        var hotspotManager = new TetheringControl.HotspotManager();
        bool result = await hotspotManager.EnableHotspot(); // 或调用DisableHotspot()
        // 根据result处理操作结果
    }
    
  3. 配置注意事项

    • WPF项目的目标平台需设置为Windows 10及以上版本(右键项目→属性→应用程序→目标框架,选择对应Windows 10版本)
    • 在Runtime Component的Package.appxmanifest中添加必要权限:internetClient、privateNetworkClientServer、networkOperatorTethering

方法二:通过PowerShell命令控制

利用Windows内置的PowerShell网络命令实现热点控制,无需依赖UWP组件:

using System.Management.Automation;
using System.Threading.Tasks;

public static class HotspotController
{
    public static async Task<bool> EnableHotspot()
    {
        using (var ps = PowerShell.Create())
        {
            ps.AddScript(@"
                $activeProfile = Get-NetConnectionProfile | Where-Object { $_.IsConnected -eq $true -and $_.NetworkCategory -eq 'Private' }
                if ($activeProfile) {
                    $tetheringStatus = Get-NetTethering -ConnectionProfileId $activeProfile.InstanceId
                    if ($tetheringStatus.TetheringState -ne 'Enabled') {
                        Start-NetTethering -ConnectionProfileId $activeProfile.InstanceId | Out-Null
                    }
                    $true
                } else {
                    $false
                }
            ");
            var result = await ps.InvokeAsync();
            return result.Count > 0 && (bool)result[0].BaseObject;
        }
    }

    public static async Task<bool> DisableHotspot()
    {
        using (var ps = PowerShell.Create())
        {
            ps.AddScript(@"
                $activeTethering = Get-NetTethering | Where-Object { $_.TetheringState -eq 'Enabled' }
                if ($activeTethering) {
                    Stop-NetTethering -ConnectionProfileId $activeTethering.ConnectionProfileId | Out-Null
                    $true
                } else {
                    $true
                }
            ");
            var result = await ps.InvokeAsync();
            return result.Count > 0 && (bool)result[0].BaseObject;
        }
    }
}

注意事项

  • 需要通过NuGet安装Microsoft.PowerShell.5.ReferenceAssemblies包,引用System.Management.Automation程序集
  • 应用必须以管理员权限运行,否则会因权限不足执行失败

关于WlanHostedNetworkStartUsing的说明

你提到的WlanHostedNetworkStartUsing()接口仅适用于旧版的netsh hostednetwork虚拟热点,这是Windows 10之前的技术,已被系统集成的移动热点(基于新Soft AP实现)替代,两者属于完全独立的机制,因此该接口无法控制当前的移动热点功能。

内容的提问来源于stack exchange,提问作者komodosp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 08:30:47