ZetCode

C# Process

last modified July 5, 2020

C# Process tutorial shows how to work with processes in C# language. C# tutorial is a comprehensive tutorial on C# language.

The Process provides access to local and remote processes and enables the developers to start and stop local system processes. The ProcessStartInfo specifies a set of values that are used when we start a process.

The Process class is part of the System.Diagnostics namespace.

C# Process simple example

In the first example, we start a console command that shows the contents of a file.

Program.cs
using System.Diagnostics;

namespace SimpleEx
{
    class Program
    {
        static void Main(string[] args)
        {
            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.

Program.cs
using System.Diagnostics;

namespace RunProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            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.

Program.cs
using System.Diagnostics;

namespace Arguments
{
    class Program
    {
        static void Main(string[] args)
        {
            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.

Program.cs
using System.Diagnostics;
using System.Threading;

namespace StartKill
{
    class Program
    {
        static void Main(string[] args)
        {
            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.

Program.cs
using System;
using System.Diagnostics;

namespace FindProcessByName
{
    class Program
    {
        static void Main(string[] args)
        {
            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
...

This is a sample output.

C# Process.GetProcesses

The Process.GetProcesses creates an array of new Process components and associates them with existing process resources.

Program.cs
using System;
using System.Diagnostics;

namespace ListProcesses
{
    class Program
    {
        static void Main(string[] args)
        {
            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 gets a stream used to read the textual output of the application.

Program.cs
using System;
using System.Diagnostics;
using System.IO;

namespace RedirectOutput
{
    class Program
    {
        static void Main(string[] args)
        {
            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 the example, we redirect the output of the ls command to the output.txt file.

psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;

Setting the UseShellExecute to false enables us to redirect input, output, and error streams. (In this context, the shell refers to the graphical shell rather than command shell such as bash or sh.)

using var process = Process.Start(psi);

We start the process with the provided info.

using StreamReader reader = process.StandardOutput;

We get the StreamReader for the standard output.

string data = reader.ReadToEnd();

We read all data with the ReadToEnd() method.

File.WriteAllText("output.txt", data);

Finally, we write the data to the file.

In this tutorial we have worked with processes in C#.

Read C# tutorial or list all C# tutorials.