How to Start a Process in C#

Muhammad Zeeshan Feb 02, 2024
  1. What is a Process in C#
  2. Use Process.Start to Start a Process in C#
  3. Use ProcessStartInfo to Start a Process in C#
  4. Use Process.GetProcesses to Start a Process in C#
  5. Use Process.GetProcessesByName to Start a Process in C#
  6. Use StandardOutput to Start a Process in C#
  7. Use Process.Kill to Stop a Process in C#
How to Start a Process in C#

This article will look at numerous ways to start a process in C#.

What is a Process in C#

A Process allows developers to access local and remote processes and initiate and terminate local system processes.

ProcessStartInfo defines a collection of values utilized when a process is started. The Process class is in the System.Diagnostics namespace.

Use Process.Start to Start a Process in C#

Follow these steps to start a process with Process.Start.

Import the libraries below.

using System;
using System.Diagnostics;

Create the Main class, and inside the Main class, write this code for starting a process (specify the program, URL or file you want to launch).

Process.Start(@"C:\Users\Shani\Desktop\process.txt");

Source code:

using System;
using System.Diagnostics;

class ProcesswithProcessStart {
  public static void Main() {
    Process.Start(@"C:\Users\Shani\Desktop\process.txt");
    Console.WriteLine("Done");
  }
}

The Start function is used to start the process. We’ll get this output after running the ProcesswithProcessStart class.

Process Start Output

Use ProcessStartInfo to Start a Process in C#

We can run any executable. However, attributes on ProcessStartInfo may be required.

Start a process using the ProcessStartInfo method by following the steps below.

Import the libraries below.

using System;
using System.Diagnostics;

The StartInfo.FileName attribute specifies the program or document to be launched. We’ll use the Microsoft Word program as an example. It’s a basic text editor and writer.

process.StartInfo.FileName = "WINWORD.EXE";

The filename is passed to StartInfo.Arguments as a parameter. Here we will launch a word file named process.docx.

process.StartInfo.Arguments = @"C:\Users\Shani\Desktop\process.docx";

Source code:

using System;
using System.Diagnostics;

Process process = new Process();

process.StartInfo.FileName = "WINWORD.EXE";
process.StartInfo.Arguments = @"C:\Users\Shani\Desktop\process.docx";
process.Start();

Output:

Process Start Info

Use Process.GetProcesses to Start a Process in C#

The method Process.GetProcesses generates a list of new Process components linked to existing process resources.

Import the libraries below.

using System;
using System.Diagnostics;

This code will produce a list of all the processes.

Process[] LinkedProcesses = Process.GetProcesses();

Following that, we obtain an array of processes.

Array.ForEach(LinkedProcesses, (process) => {
  Console.WriteLine("Process: {0} Id: {1}", process.ProcessName, process.Id);
});

ReadKey() function will hold the screen.

Console.ReadKey();

Source code:

using System;
using System.Diagnostics;

Process[] LinkedProcesses = Process.GetProcesses();

Array.ForEach(LinkedProcesses, (process) => {
  Console.WriteLine("Process: {0} Id: {1}", process.ProcessName, process.Id);
});
Console.ReadKey();

Output:

Process Get Processes

Use Process.GetProcessesByName to Start a Process in C#

The method Process.GetProcessesByName generates an array of new Process components and correlates them with existing process resources with the same name as the provided process.

Import the libraries below.

using System;
using System.Diagnostics;

All processes associated with the Internet Download Manager are generated in the example. Their IDs and process names are generated.

Process[] LinkedProcesses = Process.GetProcessesByName("InternetDownloadManager");

Users obtain the list of processes with the name “IDM” in it.

Console.WriteLine("{0} IDM LinkedProcesses", LinkedProcesses.Length);

To access all of the Processes returned by the function, we utilize a foreach loop.

The ID of each Process is accessed. Finally, we display the total number of processes discovered.

Array.ForEach(LinkedProcesses, (process) => {
  Console.WriteLine("Process: {0} Id: {1}", process.ProcessName, process.Id);
});

Source code:

using System;
using System.Diagnostics;

Process[] LinkedProcesses = Process.GetProcessesByName("InternetDownloadManager");
Console.WriteLine("{0} IDM LinkedProcesses", LinkedProcesses.Length);
Array.ForEach(LinkedProcesses, (process) => {
  Console.WriteLine("Process: {0} Id: {1}", process.ProcessName, process.Id);
});
Console.ReadKey();

Output:

Process Get Processes By Name

Use StandardOutput to Start a Process in C#

The StandardOutput property returns a pathway that may be used to read the application’s text-based output.

We may redirect the standard process output using this property (available on ProcessStartInfo). We can observe the output of one program from another.

Import the libraries below.

using System;
using System.Diagnostics;
using System.IO;

We’ll specify the name of the process we wish to output here.

Prs.StartInfo.FileName = "ipconfig.exe";

We may redirect input, output, and error streams by setting UseShellExecute to false.

Prs.StartInfo.UseShellExecute = false;
Prs.StartInfo.RedirectStandardOutput = true;

This code will start the process using the information you’ve provided.

Prs.Start();

For the standard output, we obtain the StreamReader.

StreamReader sr = Prs.StandardOutput;

The ReadToEnd() function is used to read all data.

string dataoutput = sr.ReadToEnd();

ReadKey() will hold the screen to show output.

Console.ReadKey();

Source code:

using System;
using System.Diagnostics;
using System.IO;

class StandarOutput {
  public static void Main() {
    using (Process Prs = new Process()) {
      Prs.StartInfo.FileName = "ipconfig.exe";
      Prs.StartInfo.UseShellExecute = false;
      Prs.StartInfo.RedirectStandardOutput = true;
      Prs.Start();
      StreamReader sr = Prs.StandardOutput;
      string dataoutput = sr.ReadToEnd();
      Console.WriteLine(dataoutput);
      Console.ReadKey();
    }
  }
}

Output:

Standard Output

Use Process.Kill to Stop a Process in C#

The following example launches a program and then terminates it after a few seconds.

Import the libraries below.

using System;
using System.Diagnostics;
using System.Threading;

Using Thread.Sleep(5000), the process launches Notepad and terminates it after three seconds.

Thread.Sleep(5000);

The Kill() function is used to terminate the process.

process.Kill();

Source code:

using System;
using System.Diagnostics;
using System.Threading;

class KillProcess {
  public static void Main() {
    Process process = Process.Start("notepad.exe");
    Thread.Sleep(1000);
    process.Kill();
  }
}
Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn

Related Article - Csharp Process