What is the purpose of the UseShellExecute property?

8

Developing an application in C # I came across the UseShellExecute property in the following code snippet:

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "notepad.exe";
startInfo.WindowStyle = ProcessWindowStyle.Normal;

What is the purpose of it? I read that it indicates whether the shell of the OS should be used or not and I still did not understand, what impact on the application if it is true ?

    
asked by anonymous 05.05.2017 / 15:45

2 answers

4

It's rarely interesting to use, it has more disadvantages than advantages.

As it is used as if it were a shell it even has the characteristics of how you were using Windows cmd.exe or another shell operating system pattern.

It accesses what is in PATH , runs scripts batch, and uses the association of certain names with specific applications. Generally speaking it is an unnecessary gain in almost every situation since it is more of a convenience. In general it is better to be specific and direct in what you are calling, even to avoid performing what you do not have a greater control.

He has difficulties too. Security is limited and compromised. It is not possible to redirect the result to some stream .

In this example I only see disadvantages and set true .

The documentation is yours amiga.

    
05.05.2017 / 16:03
2

This property UseShellExecute is related to the use of the ShellExecute function in Windows, that is, if you mark true , the Process class will use the ShellExecute otherwise, it will use CreateProcess .

CreateProcess

This is your case and will use the CreateProcess function, it is a much more accurate way to start a process - it does not search the path, and will allow you to redirect the default input or output of the child process. It will not work when trying to open files as will be explained in ShellExecute .

You should set UseShellExecute to false when:

  • You simply want to open a program;

ShellExecute

The ShellExecute function is used to open a specific file (even a program) - such as when typing something in the Windows Run command, for example when we want:

  • Open documents where extensions have already been associated with a program - simply type c: \ test \ fat.docx that Windows will open the WinWord program.
  • Run batch files - as in cmd.exe ;
  • Run any command in the PATH;

Use when you want to open documents, urls or batch files etc ... instead of having to explicitly pass the path from where the program was installed.

    
05.05.2017 / 16:11