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.
Start 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.
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
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.
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.
Detail We use a 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.
Here We use Process.GetProcessesByName in a while-true loop. We use the argument "chrome" to GetProcessesByName.
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.
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.
Detail 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.
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.
Detail We start the Notepad program, and then wait one second. Finally, we call the Kill method on the process variable.
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();
}
}
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 11, 2023 (simplify).