In C# Process.Start()
calls external applications. We can start an EXE as a process. We must pass the target command along with the desired arguments.
The Process type is platform-neutral: we can use it to call programs on Windows, Linux or macOS. Code is resilient and cross-platform.
We add the System.Diagnostics
namespace. Our sample project is a C# .NET Core console application. But these steps will work in many configurations.
using System; using System.Diagnostics; class Program { static void Main() { // Place exe named cwebp.exe at C directory root. Process.Start(@"c:\cwebp.exe", @"-quiet c:\programs\test.png -o c:\programs\test.webp"); Console.WriteLine("DONE"); } }DONE
We can run any executable. But we may need to use properties on ProcessStartInfo
. Here we run a program called cwebp.exe—it converts a certain image format.
ProcessStartInfo
. We use CreateNoWindow
and UseShellExecute
to control some command-line options.Process.Start
, and then call WaitForExit
to wait for the executable to finish its task.using System; using System.Diagnostics; class Program { static void Main() { string inputFile = @"C:\programs\test.png"; string outputFile = @"C:\programs\test.webp"; // Part 1: use ProcessStartInfo class. ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = false; startInfo.UseShellExecute = false; startInfo.FileName = @"c:\cwebp.exe"; startInfo.WindowStyle = ProcessWindowStyle.Hidden; // Part 2: set arguments. startInfo.Arguments = "-q 30 " + inputFile + " -o " + outputFile; try { // Part 3: start with the info we specified. // ... Call WaitForExit. using (Process exeProcess = Process.Start(startInfo)) { exeProcess.WaitForExit(); } } catch { // Log error. } Console.WriteLine("DONE"); } }Saving file 'C:\programs\test.webp' File: C:\programs\test.png Dimension: 300 x 175 Output: 3884 bytes Y-U-V-All-PSNR 35.98 39.98 36.86 36.59 dB block count: intra4: 124 intra16: 85 (-> 40.67%) skipped block: 33 (15.79%) bytes used: header: 130 (3.3%) mode-partition: 530 (13.6%) Residuals bytes |segment 1|segment 2|segment 3|segment 4| total macroblocks: | 1%| 9%| 51%| 36%| 209 quantizer: | 68 | 63 | 55 | 43 | filter level: | 24 | 15 | 11 | 9 | DONE
GetProcesses
This gets an array of all the processes currently open on the system. We can loop over and test for running programs.
Process.GetProcesses
looks at all processes currently running. With this method, we enumerate the active processes.GetProcesses
receives no arguments or one argument of the target machine name (not shown). It returns a Process array.foreach
-loop to access all the Processes returned by the method. We access each Process's Id.using System; using System.Diagnostics; class Program { static void Main() { // Show all processes on the local computer. Process[] processes = Process.GetProcesses(); // Display count. Console.WriteLine("Count: {0}", processes.Length); // Loop over processes. foreach (Process process in processes) { Console.WriteLine(process.Id); } } }Count: 70 388 5312 1564 972 2152 936 3132....
GetProcessesByName
This method returns an array of Process objects. Its functionality can be derived by combining other methods such as GetProcesses
.
Process.GetProcessesByName
in a while-true
loop. We use the argument "chrome" to GetProcessesByName
.string
"chrome.exe" as the argument.using System; using System.Diagnostics; using System.Threading; class Program { static void Main() { while (true) { // Omit the exe part. Process[] chromes = Process.GetProcessesByName("chrome"); Console.WriteLine("{0} chrome processes", chromes.Length); Thread.Sleep(5000); } } }0 chrome processes 3 chrome processes 4 chrome processes 5 chrome processes 5 chrome processes 5 chrome processes
RedirectStandardOutput
With this property (found on ProcessStartInfo
) we can redirect the standard output of Process. We can see program output within another program.
RedirectStandardOutput
eliminates the need for output files. It allows us to use a console program directly inside a C# program.StreamReader
. With ReadToEnd()
we can read the entire output of an EXE into a string
.UseShellExecute
to false and RedirectStandardOutput
to true.FileName
property) before calling Process.Start
.using System; using System.Diagnostics; using System.IO; class Program { static void Main() { // // Set up the process with the ProcessStartInfo class. // ProcessStartInfo start = new ProcessStartInfo(); start.FileName = @"C:\7za.exe"; // Specify exe name. start.UseShellExecute = false; start.RedirectStandardOutput = true; // // Start the process. // using (Process process = Process.Start(start)) { // // Read in all the text from the process with the StreamReader. // using (StreamReader reader = process.StandardOutput) { string result = reader.ReadToEnd(); Console.Write(result); } } } }7-Zip (A) 4.60 beta Copyright (c) 1999-2008 Igor Pavlov 2008-08-19 Usage: 7za <command> [<switches>...] <archive_name> [<file_names>...] [<@listfiles...>]
It is possible to Kill or terminate a Process that we acquire a reference to. The process is not given a chance to continue running.
using System.Diagnostics; using System.Threading; class Program { static void Main() { // Start notepad. Process process = Process.Start("notepad.exe"); // Wait one second. Thread.Sleep(1000); // End notepad. process.Kill(); } }
The Process type allows an external access to the operating system. With Process, we can run any EXE. We can pass command-line arguments to these programs.