0

We have a .NET 4.5.2 application we're bringing forward to .NET 5.0.

We have a bit where we write a file and want to open it in the user's configured default editor.

In .NET 4.5, this was easy:

// If we're showing SQL, load the output file
if (myOptions.outputSql)
{
    if (File.Exists(myOptions.outputFilename))
        Process.Start(myOptions.outputFilename);
}

But in .NET 5.0 this throws an exception:

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

What I want to do is to spawn whatever is the default application for the specified file, and pass it the file. In .NET 4.5, ProcessStart() would do this. In .NET 5.0 it no longer does.

So, how do I accomplish this, now?

Jeff Dege
  • 11,190
  • 22
  • 96
  • 165
  • You need to set `ProcessStartInfo.UseShellExecute = true` – canton7 Mar 05 '21 at 17:34
  • 1
    Does this help you? [.Net Core 2.0 Process.Start throws “The specified executable is not a valid application for this OS platform”](https://stackoverflow.com/questions/46808315/net-core-2-0-process-start-throws-the-specified-executable-is-not-a-valid-appl) –  Mar 05 '21 at 17:34
  • 3
    Don't look at the accepted answer of @Amy's link (it's a shame when the accepted answer is the worst one), but the ones which talk about `UseShellExecute` are correct. The default for this property was changed from `true` to `false` in .NET Core. – canton7 Mar 05 '21 at 17:37

0 Answers0