What is the useshellexecute property for?

Asked

Viewed 590 times

8

Developing an application in c# I came across the property UseShellExecute 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 its purpose? I read that it indicates if the shell of the OS should be used or not and I continued without understanding, what the impact on the application should it be true?

2 answers

5


Its use is rarely interesting, it has more disadvantages than advantages.

As it is used as if it were one shell even it has the characteristics of how you were using the cmd.exe windows or other shell operating system standard.

He accesses what’s on PATH, executes scripts batch, and uses the association of certain names with specific applications. Overall it is an unnecessary gain in almost every situation as it is more of a convenience. In general it is better to be specific and direct in what you are calling, even to avoid executing what you do not have a greater control.

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

In this example I only see disadvantages and put true.

To documentation is your friend.

2

That property UseShellExecute is related to the use of the function ShellExecute existing in Windows ie if you mark as true the class Process will use the function ShellExecute, otherwise, use the CreateProcess.

Createprocess

This is your case and the function will be used CreateProcess, 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 on ShellExecute.

You must define UseShellExecute as false when:

  • Simply open a program;

Shellexecute

The function ShellExecute is used to open a specific file (even a program) - as when we type 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 take charge of opening the Winword program.
  • Run batch files - as in cmd.exe;
  • Execute any command in 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.