C# 如何在.NETCore3.1WPF中运行交互Shell
我正在尝试运行Shell命令,以在连接或断开vpn时启用或禁用防火墙权限,并且我使用microsoft.visualbasic来启用或禁用防火墙权限,这在Visual studio 2015中运行良好,但现在我在Visual studio 2019.Net Core中以及在使用microsoft.visualbasic时也在做同样的事情将Shell方法中的错误显示为 CS0117“交互”不包含“Shell”的定义 这就是我试图运行的代码C# 如何在.NETCore3.1WPF中运行交互Shell,c#,wpf,visual-studio-2019,.net-core-3.1,C#,Wpf,Visual Studio 2019,.net Core 3.1,我正在尝试运行Shell命令,以在连接或断开vpn时启用或禁用防火墙权限,并且我使用microsoft.visualbasic来启用或禁用防火墙权限,这在Visual studio 2015中运行良好,但现在我在Visual studio 2019.Net Core中以及在使用microsoft.visualbasic时也在做同样的事情将Shell方法中的错误显示为 CS0117“交互”不包含“Shell”的定义 这就是我试图运行的代码 using System; using System.Co
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using System.Diagnostics;
using System.Collections;
using CasVPN.Model;
namespace CasVPN.Classes
{
class Global
{
public static void Disable_Network()
{
string value = "netsh advfirewall firewall add rule name = Block All Traffic dir =in action = allow port enable = yes remoteip = 5, 46, 48 - 1722, 1724 - 65535,LocalSubnet profile = public";
Interaction.Shell("cmd.exe /c" + value);
}
}
}
根据,Interaction.Shell仅在.NETFramework中可用,不属于.NETCore的一部分
您需要将应用程序更改为目标.NET Framework,或使用运行命令的替代方法,如。1。运行
netsh.exe
2不需要cmd.exe的实例。您需要使用。首先为什么要使用Interaction.Shell
?只需使用Process.Start(值)
或进程。启动(“netsh”,“advfirewall-firewall…”)
。每个人都忽略了全局变量的事实,唉。顺便说一句,当您使用Process.Start()时;而且它不工作设置UseShellExecute=true代码>。