WPF Using .Net 3.1 - Process.Start Excel File Error: "The specified executable is not a valid application for this OS platform."
Asked Answered
B

2

2

I am trying to open an Excel file (.xlsx) within a WPF .NET Core 3.1 app. Using Winforms, I am able to do

process.start("Resources/test.xlsx")

and the file will open.

In the WPF application doing the same thing results in an error

The specified executable is not a valid application for this OS platform

I am using the same code and opening the same file using both apps. Only the Winforms app will work, WPF will throw that error.

Is there a new way of opening files is handled using System.Diagnostics.Process.Start in .Net 3?

Blakeslee answered 1/4, 2020 at 12:4 Comment(0)
B
7

Looking around further, it seems that in .Net Core the UseShellExecute value is defaulted to false. Manually setting it to true fixed the issue. Here is the blog article I used to discover this fix: https://jeremybytes.blogspot.com/2019/08/converting-net-framework-to-net-core.html

Blakeslee answered 1/4, 2020 at 12:18 Comment(0)
R
8

Something likes this

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);
}
Rodie answered 5/12, 2021 at 8:7 Comment(1)
Thanks for putting in the code for the question (forgot I just put in a link which is bad...). +1Blakeslee
B
7

Looking around further, it seems that in .Net Core the UseShellExecute value is defaulted to false. Manually setting it to true fixed the issue. Here is the blog article I used to discover this fix: https://jeremybytes.blogspot.com/2019/08/converting-net-framework-to-net-core.html

Blakeslee answered 1/4, 2020 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.