106

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.

Jyunhao Shih
  • 1,287
  • 2
  • 8
  • 9

6 Answers6

195

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.

See also breaking changes.

Xan-Kun Clark-Davis
  • 2,664
  • 2
  • 27
  • 38
Mathew
  • 4,297
  • 4
  • 29
  • 38
  • 3
    Changing `UseShellExecute` from `false` to `true` works for me :-) – Greg Trevellick Dec 31 '18 at 19:06
  • 2
    See Microsoft [breaking changes doc here](https://learn.microsoft.com/en-us/dotnet/core/compatibility/fx-core) – Steve Jun 04 '21 at 22:01
  • I was driving myself crazy wondering why calling Process.Start() on local HTML files didn't work even though everybody claimed it worked. This solves the issue. – Vincent La Jun 02 '22 at 19:23
  • This work for me, but why does it work with "Open width" ? The "OLD WAY" `System.Diagnostics.Process.Start("rundll32.exe", "shell32.dll, OpenAs_RunDLL " + item.FileName);` – Wolfgang Schorge Feb 22 '23 at 15:14
52

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 });
NetMage
  • 26,163
  • 3
  • 34
  • 55
Liam
  • 5,033
  • 2
  • 30
  • 39
  • 1
    converted an app from net48 to netcoreapp3.1, "UseShellExecute = true" was needed on dotnetcore (running on windows) – huzle Mar 13 '20 at 21:19
  • I used .msg file didn't open. i am getting this error "System.ComponentModel.Win32Exception: 'The system cannot find the file specified.' ". – Kumar Shanmugam Jun 08 '21 at 15:01
23

In case this bothers you as well:

For those of us that are used to simply calling Process.Start(fileName); the above syntax may give us anxiety... So may I add that you can write it in a single line of code?

new Process { StartInfo = new ProcessStartInfo(fileName) { UseShellExecute = true } }.Start();
Yosef Bernal
  • 1,006
  • 9
  • 20
  • 2
    This should be the accepted answer works great – dgxhubbard Mar 12 '21 at 17:44
  • I concur with @dgxhubbard that this should be the accepted answer because this answer suppresses the command window while the accepted answer does not (and the command window displays while the application is open). – J Weezy Mar 18 '21 at 19:57
20

You have to execute cmd.exe

var proc = Process.Start(@"cmd.exe ",@"/c C:\Users\user2\Desktop\XXXX.reg")

don't forget the /c

owairc
  • 1,890
  • 1
  • 10
  • 9
  • 4
    This works, but keeps open a cmd window until de opened application is close. For example, if I want to open a doc file, the cmd window keeps opened until I close Word. It is better if use another solution with UseShellExecute. – Álvaro García Oct 11 '19 at 11:26
  • 3
    This should not be the accepted answer. The use of ProcessStartInfo with UseShellExecute = true is better. – Paul Williams May 21 '21 at 17:35
-1

use this to open a file

new ProcessStartInfo(@"C:\Temp\1.txt").StartProcess();

need this extension method

public static class UT
{
    public static Process StartProcess(this ProcessStartInfo psi, bool useShellExecute = true)
    {
        psi.UseShellExecute = useShellExecute;
        return Process.Start(psi);
    }
}
Rm558
  • 4,621
  • 3
  • 38
  • 43
-1
            string itemseleccionado = lbdatos.SelectedItem.ToString();
            var p = new Process();
            p.StartInfo = new ProcessStartInfo(itemseleccionado)
            {
                UseShellExecute = true
            };
            p.Start();
  • 3
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Aug 13 '22 at 04:24
  • No explanation + anwser already given = downvote – Antonio Rodríguez Dec 28 '22 at 09:11