问题描述
// 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。
或者如果我禁用 UseShellExecute
process.StartInfo.UseShellExecute = false;
由于管理员权限,程序无法执行
Sooo...如何在 UWP 中以管理员身份执行 msi?
解决方法
首先,这里提到-ProcessStartInfo.UseShellExecute Property 设置 ProcessStartInfo.UseShellExecute 属性将引发 PlatformNotSupportedException 如果您在 UWP 应用程序中执行此操作。你得到的行为是预期的。其次,UWP 应用不支持 Process.Start Method。要从您的 UWP 应用运行 exe 文件,我建议您使用 FullTrustProcessLauncher Class。
首先,您需要复制exe文件并将其保存在UWP应用程序包中,例如Assets文件夹。然后请转到清单文件并添加runFullTrust 受限功能。(App capability)。接下来,为 UWP 项目添加 Windows 桌面扩展 的引用。最后,您可以调用 await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
从您的应用中启动 exe 文件。