Jak*_*360 2 c# wpf system.diagnostics
.xlsx我正在尝试在 WPF .NET Core 3.1 应用程序中打开 Excel 文件 ( )。使用 Winforms,我能够做到
process.start("Resources/test.xlsx")
Run Code Online (Sandbox Code Playgroud)
文件将会打开。
在 WPF 应用程序中执行相同的操作会导致错误
指定的可执行文件不是该操作系统平台的有效应用程序
我使用相同的代码并使用两个应用程序打开相同的文件。只有 Winforms 应用程序可以工作,WPF 会抛出该错误。
.Net 3 中是否有一种新的打开文件方式System.Diagnostics.Process.Start?
有东西喜欢这个
private void barButtonItem4_ItemClick(object sender, ItemClickEventArgs e)
{
// Old way (.NET Framework 4.8)
// string path = "Resources/test.xlsx";
// Process.Start(path);
// New way (.NET 6)
ProcessStartInfo psInfo = new ProcessStartInfo
{
FileName = "Resources/test.xlsx",
UseShellExecute = true
};
Process.Start(psInfo);
}
Run Code Online (Sandbox Code Playgroud)
进一步查看,似乎在 .Net Core 中该UseShellExecute值默认为 false。手动将其设置为 true 解决了该问题。这是我用来发现此修复的博客文章:https://jeremybytes.blogspot.com/2019/08/converting-net-framework-to-net-core.html