C# Process
last modified July 5, 2023
In this article, we explore how to work with processes in C# using the
System.Diagnostics
namespace.
The Process
class provides access to both local and remote
processes, allowing developers to start, stop, monitor, and interact with system
processes. Whether it's launching an external application, capturing process
output, or managing running tasks, the Process
class offers a
comprehensive set of functionalities.
To configure process execution, C# provides the ProcessStartInfo
class, which specifies various settings such as:
- FileName: Defines the executable or command to run.
- Arguments: Passes command-line arguments to the process.
- UseShellExecute: Determines whether to use the system shell to start the process.
- RedirectStandardOutput: Enables capturing the process output.
- CreateNoWindow: Specifies whether to run the process in a hidden window.
The Process
class is part of the System.Diagnostics
namespace, which contains various tools for debugging, monitoring, and
performance analysis.
With the ability to start, manage, and terminate system processes, C# developers can efficiently interact with external applications, automate tasks, and analyze system activity.
C# Process simple example
In the first example, we start a console command that shows the contents of a file.
using System.Diagnostics; Process.Start("cat", @"C:\Users\Jano\Documents\words.txt");
The example outputs the contents of the words.txt
file with
the cat
command. The command is not part of the Windows OS by
default; it is installed with the git tools (see gitforwindows.org).
Process.Start("cat", @"C:\Users\Jano\Documents\words.txt");
The process is started with the Start
method.
$ dotnet run sky cloud falcon owl crane
C# Process run program
In the following example, we run a GUI program.
using System.Diagnostics; using var process = new Process(); process.StartInfo.FileName = "notepad.exe"; process.Start();
In the example, we run the Notepad program. It is a standard, small text editor.
process.StartInfo.FileName = "notepad.exe";
The StartInfo.FileName
property bets or sets the application or
document to start.
using System.Diagnostics; using var process = new Process(); process.StartInfo.FileName = "notepad.exe"; process.StartInfo.Arguments = @"C:\Users\Jano\Documents\words.txt"; process.Start();
We use the StartInfo.Arguments
to pass the file name to be opened.
C# start and kill program
The next example starts a program and kills it after a few seconds.
using System.Diagnostics; using var process = Process.Start("notepad.exe"); Thread.Sleep(3000); process.Kill();
The example starts Notepad, sleeps for three seconds and kills the process
with the Kill
method.
C# Process.GetProcessesByName
The Process.GetProcessesByName
creates an array of new Process
components and associates them with the existing process resources that all
share the specified process name.
using System.Diagnostics; Process[] processes = Process.GetProcessesByName("Firefox"); Console.WriteLine("{0} Firefox processes", processes.Length); Array.ForEach(processes, (process) => { Console.WriteLine("Process: {0} Id: {1}", process.ProcessName, process.Id); });
In the example, we find all processes belonging to the Firefox. We list their Ids and process names.
Process[] processes = Process.GetProcessesByName("Firefox");
We get the array of processes by the name of "Firefox".
Console.WriteLine("{0} Firefox processes", processes.Length);
We print the number of processes found.
Array.ForEach(processes, (process) => { Console.WriteLine("Process: {0} Id: {1}", process.ProcessName, process.Id); });
We list the processes with the ForEach
method.
$ dotnet run 12 Firefox processes Process: firefox Id: 10056 Process: firefox Id: 13016 Process: firefox Id: 12944 Process: firefox Id: 10124 Process: firefox Id: 15556 ...
C# Process.GetProcesses
The Process.GetProcesses
creates an array of new Process
components and associates them with existing process resources.
using System.Diagnostics; Process[] processes = Process.GetProcesses(); Array.ForEach(processes, (process) => { Console.WriteLine("Process: {0} Id: {1}", process.ProcessName, process.Id); });
The example list all processes.
Process[] processes = Process.GetProcesses();
We get the array of processes.
Array.ForEach(processes, (process) => { Console.WriteLine("Process: {0} Id: {1}", process.ProcessName, process.Id); });
We iterate over the array and print the process names and Ids.
C# Process redirect output
The StandardOutput
property provides a stream for reading the textual output generated by the application.
using System.Diagnostics; var psi = new ProcessStartInfo(); psi.FileName = "ls"; psi.UseShellExecute = false; psi.RedirectStandardOutput = true; using var process = Process.Start(psi); using StreamReader reader = process.StandardOutput; string data = reader.ReadToEnd(); File.WriteAllText("output.txt", data);
In this example, we redirect the output of the ls
command to the
output.txt
file.
psi.UseShellExecute = false; psi.RedirectStandardOutput = true;
Setting UseShellExecute
to false
allows us to redirect
the input, output, and error streams. (Here, the shell refers to the graphical
shell, not a command shell such as bash or sh.)
using var process = Process.Start(psi);
We start the process using the specified ProcessStartInfo
settings.
using StreamReader reader = process.StandardOutput;
We obtain a StreamReader
for the standard output stream.
string data = reader.ReadToEnd();
We read all the output data using the ReadToEnd
method.
File.WriteAllText("output.txt", data);
Finally, we write the captured output to the file.
Source
Process class - language reference
In this article we have worked with processes in C#.
Author
List all C# tutorials.