C uses the Process class to call an external exe program

  • 2021-09-20 21:18:32
  • OfStack

Calling executable programs is often used when writing programs. This article will briefly introduce the method of calling exe by C #. In C #, process operations are performed through the Process class. The Process class is in the System. Diagnostics package.

Example 1

using System.Diagnostics;
Process p = Process.Start("notepad.exe");
p.WaitForExit();// Key, wait for the external program to exit before executing

Notepad program can be called through the above code. Note that if you are not calling the system program, you need to enter the full path.

Example 2

When the cmd program needs to be called, using the above call method will pop up an annoying black window. If you want to eliminate it, you need to set it up in more detail.

The StartInfo attribute of the Process class contains 1 process startup information, of which several are more important

FileName executable file name
Arguments program parameter, entered in string form
Does CreateNoWindow not need to create a window
Does UseShellExecute require system shell caller

Through the above several parameters, you can make the annoying black screen disappear

System.Diagnostics.Process exep = new System.Diagnostics.Process();
exep.StartInfo.FileName = binStr;
exep.StartInfo.Arguments = cmdStr;
exep.StartInfo.CreateNoWindow = true;
exep.StartInfo.UseShellExecute = false;
exep.Start();
exep.WaitForExit();// Key, wait for the external program to exit before executing

Or

System.Diagnostics.Process exep = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = binStr;
startInfo.Arguments = cmdStr;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
exep.Start(startInfo);
exep.WaitForExit();// Key, wait for the external program to exit before executing


Related articles: