UWP 不想以管理员身份执行进程

UWP don't want to execute process as admin

我有代码:

// Execute msi
Process process = new Process();
process.StartInfo.FileName = "msiexec";
process.StartInfo.WorkingDirectory = Path.GetDirectoryName(msiPath);
process.StartInfo.Arguments = $"  /passive /i {Path.GetFileName(msiPath)}";
process.StartInfo.Verb = "runas";
process.StartInfo.UseShellExecute = true;
process.Start();
process.WaitForExit();

但是我有错误:

UseShellExecute is not supported on this platform.

或者如果我禁用 UseShellExecute

process.StartInfo.UseShellExecute = false;

由于管理员权限,程序未执行

Sooo...我如何在 UWP 中以管理员身份执行 msi?

首先在这里提到-ProcessStartInfo.UseShellExecute Property that set the ProcessStartInfo.UseShellExecute Property will raise a PlatformNotSupportedException if you do it in UWP apps. The behavior you got is expected. Second, Process.Start Method is not supported in UWP apps. To run the exe file from your UWP app, I'd suggest you use FullTrustProcessLauncher Class.

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