草庐IT

c# - .Net Core 2.0 Process.Start 抛出 "The specified executable is not a valid application for this OS platform"

coder 2023-07-12 原文

我需要让 .reg 文件和 .msi 文件使用与用户 Windows 上关联的这两种文件类型的任何可执行文件自动执行。

.NET Core 2.0 Process.Start(string fileName) docs说: “文件名不需要代表可执行文件。它可以是扩展名与系统上安装的应用程序相关联的任何文件类型。”

不过

using(var proc = Process.Start(@"C:\Users\user2\Desktop\XXXX.reg")) { } //.msi also

给我

System.ComponentModel.Win32Exception (0x80004005): The specified executable is not a valid application for this OS platform.
   at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(String fileName)

ErrorCode 和 HResult -2147467259,以及 NativeErrorCode 193。

相同的代码在 .Net Framework 3.5 或 4 控制台应用程序中确实有效。

我无法将确切的 exe 文件路径指定为方法的参数,因为用户的环境是可变的(包括 Windows 版本)并且不受我的控制。这也是为什么我需要将程序移植到 .Net Core,试图让它像 SCD 一样工作。控制台应用程序,这样就不需要安装特定的 .Net Framework 或 .NET Core 版本。

在 Visual Studio 调试运行和发布为 win-x86 SCD 时都会抛出异常。我的 PC 是 Win7 64 位,我确信 .reg 和 .msi 与通常的 Windows PC 一样与常规程序相关联。

有解决办法吗?感谢您的帮助。

最佳答案

您还可以将 ProcessStartInfoUseShellExecute 属性设置为 true

var p = new Process();
p.StartInfo = new ProcessStartInfo(@"C:\Users\user2\Desktop\XXXX.reg")
{ 
    UseShellExecute = true 
};
p.Start();

似乎是 .net Core 中的更改,如文档所述here .

另见 breaking changes .

关于c# - .Net Core 2.0 Process.Start 抛出 "The specified executable is not a valid application for this OS platform",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46808315/

有关c# - .Net Core 2.0 Process.Start 抛出 "The specified executable is not a valid application for this OS platform"的更多相关文章

随机推荐