2

In this answer the author @Abel suggests using ShellExecute whenever Process.Start will not function.

When would I use ShellExecute - I did never have the situation that Process.Start did not work?

Furthermore, are there any advantages for using ShellExecute over Process.Start?

Community
  • 1
  • 1
dhh
  • 4,289
  • 8
  • 42
  • 59

1 Answers1

4

are there any advantages for using ShellExecute over Process.Start

First, you need to understand what ShellExecute does. From ProcessStartInfo.UseShellExecute:

When you use the operating system shell to start processes, you can start any document (which is any registered file type associated with an executable that has a default open action) and perform operations on the file, such as printing, by using the Process object. When UseShellExecute is false, you can start only executables by using the Process object.

This means that it will allow you to open any file that has an assosicated file type, such as a given word document. Otherwise, you can only invoke executables. If you set this flag to true in ProcessStartInfo, internally, Process.Start will invoke the same WinAPI call:

public bool Start()
{
    Close();
    ProcessStartInfo startInfo = StartInfo;
    if (startInfo.FileName.Length == 0) 
        throw new InvalidOperationException(SR.GetString(SR.FileNameMissing));

    if (startInfo.UseShellExecute) 
    {            
        return StartWithShellExecuteEx(startInfo);
    } 
    else
    {
        return StartWithCreateProcess(startInfo);
    }
}

When you invoke ShellExecute, you're using PInvoke to directly call WinAPI. With Process.Start, you're simply invoking the managed wrapper, which is usually more convenient to use.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321