Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core 2.0 Process.Start throws "The specified executable is not a valid application for this OS platform"

I need to let a .reg file and a .msi file execute automatically using whatever executables these two file types associated with on user's Windows.

.NET Core 2.0 Process.Start(string fileName) docs says: "the file name does not need to represent an executable file. It can be of any file type for which the extension has been associated with an application installed on the system."

However

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

gives me

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) 

with ErrorCode and HResult -2147467259, and NativeErrorCode 193.

The same code did work in .Net Framework 3.5 or 4 console app.

I can't specify exact exe file paths as the method's parameter since users' environments are variant (including Windows versions) and out of my control. That's also why I need to port the program to .Net Core, trying to make it work as SCD console app so that installation of specific .Net Framework or .NET Core version is not required.

The exception is thrown both in Visual Studio debugging run and when published as win-x86 SCD. My PC is Win7 64bit and I'm sure .reg and .msi are associated with regular programs as usual Windows PC does.

Is there solution for this? Any help is appreciated.

like image 524
user3187356 Avatar asked Oct 18 '17 10:10

user3187356


People also ask

What is system diagnostics process start?

Start(String)Starts a process resource by specifying the name of a document or application file and associates the resource with a new Process component. public: static System::Diagnostics::Process ^ Start(System::String ^ fileName); C# Copy.

How do I start a process in VB net?

Start another application using your . NET code As a . NET method, Start has a series of overloads, which are different sets of parameters that determine exactly what the method does. The overloads let you specify just about any set of parameters that you might want to pass to another process when it starts.

What is process start?

Start(ProcessStartInfo) Starts the process resource that is specified by the parameter containing process start information (for example, the file name of the process to start) and associates the resource with a new Process component.


2 Answers

You can also set the UseShellExecute property of ProcessStartInfo to true

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

Seems to be a change in .net Core, as documented here.

like image 192
Mathew Avatar answered Sep 17 '22 19:09

Mathew


You can set UseShellExecute to true and include this and your path in a ProcessStartInfo object:

Process.Start(new ProcessStartInfo(@"C:\Users\user2\Desktop\XXXX.reg") { UseShellExecute = true }); 
like image 27
Liam Avatar answered Sep 21 '22 19:09

Liam