Start and Kill Processes in C#

In the .NET Framework, the System.Diagnostics namespace provides classes that enable interaction with system processes, event logs, and performance counters. One important aspect of this namespace is the ability to work with system processes using the Process component.

The Process component allows you to obtain a list of running processes on the system and start new processes. Each system process is uniquely identified by its process identifier (PID). By utilizing the Process component, you can perform various operations related to system processes.

Process[] _proceses = null; _proceses = Process.GetProcessesByName("calc");

Process.GetProcessesByName("calc") method

The Process.GetProcessesByName("calc") method retrieves an array of new Process components associated with all the process resources on the local computer that share the specified process name, in this case, "calc" (referring to the Calculator application).

This way, enable you to get all the information about the Calculator app, which is currently running, and also access them using the program. We focus on carrying out activities such as retrieving process information and showing where the processes are, while at the same time, collecting the performance metrics.

Full Source C#
using System; using System.Windows.Forms; using System.Diagnostics; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("calc"); } private void button2_Click(object sender, EventArgs e) { Process[] _proceses = null; _proceses = Process.GetProcessesByName("calc"); foreach (Process proces in _proceses) { proces.Kill(); } } } }

Conclusion

The Process component offers various properties and methods for working with system processes. These include properties to access process information (e.g., ProcessName, Id, StartTime) and methods to start new processes (e.g., Start, WaitForExit).