Win32Exception访问被拒绝



所以我试图在C#中运行python代码。但当我试图执行命令时,我收到一条错误消息:

System.ComponentModel.Win32Exception: 'Access Denied'

代码:

private void run_cmd(string cmd, int args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "C://Users//user//AppData//Local//Microsoft//WindowsApps//python.exe";
start.Arguments = string.Format("{0} {1}", cmd, args);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
}

错误发生在:

using (Process process = Process.Start(start))

UWP应用程序不支持Process.Start方法。要从UWP应用程序运行exe文件,我建议您使用FullTrustProcessLauncher类。

首先,您需要复制exe文件并将其保存在UWP应用程序包中,例如Assets文件夹。然后,请转到清单文件并添加runFullTrust受限功能。(应用程序功能(。接下来,为UWP项目添加Windows桌面扩展的引用。最后,您可以调用await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();从您的应用程序中启动exe文件。

最新更新