【问题标题】:Win32Exception Access DeniedWin32Exception 访问被拒绝
【发布时间】:2021-09-04 14:02:30
【问题描述】:

所以我试图在 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))

【问题讨论】:

  • 该错误通常意味着权限断开。为什么“python”安装在 AppData 下 - 它不应该在“C:\\Program files”下吗?是否为所有用户安装了“python”?您的应用是服务还是需要以管理员身份运行的东西?
  • 你的应用是 UWP 应用吗?
  • 是的,该应用是 uwp 应用
  • 我尝试从 windows 商店运行 python 和在我的 venv 中运行 python。但两者都给了我同样的错误。到目前为止,我的应用程序非常基本,所以我认为我不需要任何特权来运行它。
  • 你检查我的答案了吗?它解决了你的问题吗?

标签: c# windows uwp components system


【解决方案1】:

Process.Start Method 在 UWP 应用中不受支持。要从你的 UWP 应用程序运行 exe 文件,我建议你使用FullTrustProcessLauncher Class

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-04
    • 2017-09-15
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多