C# Process Examples (Process.Start)Create a Process to start external EXEs. Include the System.Diagnostics namespace and call Process.Start.
dot net perls

Process. 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.

Platform notes. The Process type is platform-neutral: we can use it to call programs on Windows, Linux or macOS. Code is resilient and cross-platform.

Exe example. We add the System.Diagnostics namespace. Our sample project is a C# .NET Core console application. But these steps will work in many configurations.

First Place the exe you wish to run in a known location on the disk—we use the C root directory here.

Then Read the exe documentation to find out what arguments to pass. Here we use cwebp.exe, which converts to WebP images.

C# program that uses exe with Process.Start
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"); } }

Exe example 2. 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.

Part 1 This example first creates a ProcessStartInfo. We use CreateNoWindow and UseShellExecute to control some command-line options.

Part 2 We set some arguments to indicate to the executable what directories were are using.

Part 3 We invoke Process.Start, and then call WaitForExit to wait for the executable to finish its task.

C# program that uses ProcessStartInfo
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.

Note Process.GetProcesses looks at all processes currently running. With this method, we enumerate the active processes.

Info GetProcesses receives no arguments or one argument of the target machine name (not shown). It returns a Process array.

Foreach We use a foreach-loop to access all the Processes returned by the method. We access each Process's Id.


C# program that uses GetProcesses method
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.

Here We use Process.GetProcessesByName in a while-true loop. We use the argument "chrome" to GetProcessesByName.


Name This returns an array of all processes with name "chrome." Then, we sleep for 5 seconds before calling the method again.


Result Because of its multi-process model, Chrome can run in several processes at once.

Tip Avoid the "exe" part. The method did not work when I tried using the string "chrome.exe" as the argument.

C# program that uses GetProcessesByName
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.

Info RedirectStandardOutput eliminates the need for output files. It allows us to use a console program directly inside a C# program.

StreamReader We redirect into a StreamReader. With ReadToEnd() we can read the entire output of an EXE into a string.


Tip To redirect the output of a process, set UseShellExecute to false and RedirectStandardOutput to true.

Also You must specify the file name (with the FileName property) before calling Process.Start.

C# program that redirects standard output
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...>]

Kill. 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.

Example We start the Notepad program, and then wait one second. Finally, we call the Kill method on the process variable.

Tip It is probably more common to use the Kill method to search for and stop applications that are not working.

So First loop through the running processes. Then, you can acquire a reference to the process and call Kill.

C# program that uses Process Kill method
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(); } }

ProcessStartInfo. This is a class in System.Diagnostics. It stores information about the process—specifically how the process is started and its configuration.

Process FileName. This is a property that indicates the program or file you want to run. It can be a program such as "WINWORD.EXE". Sometimes we can just specify a file name.

Process Arguments. Thir property stores the arguments, such as -flags or filename. This is a string property. We can assign a string to it.

Window properties. CreateNoWindow allows you to silently run a command line program. It does not flash a console window. Use WindowStyle to set windows as hidden.

Static methods. Many methods are available on the Process type. Some are instance (called on the instance) and others are static (called on the type).


Main. We can combine the Process.Start method with a console program that receives a parameter list. The program handles a string args array on invocation.

Main args

Some EXEs. We can use EXE programs like brotli.exe, 7z.exe, cwebp.exe, and avifenc.exe to compress text files and images. Some examples are available.

7-Zip Executable


A review. 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.

System.Diagnostics. This namespace makes many things possible. With Process.Start, we can run separate programs concurrently with no threading constructs.

© 2007-2021 sam allen. send bug reports to info@dotnetperls.com.