Start and Kill Processes in C#
A system process is uniquely identified on the system by its process identifier. Using the Process component, you can obtain a list of the processes that are running, or you can start a new process in the system. The System.Diagnostics namespace provides classes that allow you to interact with system processes, event logs, and performance counters.
Process[] _proceses = null;
_proceses = Process.GetProcessesByName("calc");
Process.GetProcessesByName("calc") - Creates an array of new Process components and associates them with all the process resources on the local computer that share the specified process name. The following C# program allows to start multiple calculator applications and later it kill the same instances of all calculator applications.
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();
}
}
}
}
Related Topics
- C# Types
- C# boxing and unboxing
- C# DataTypes
- C# type conversions
- C# Access Modifiers , CSharp Access Specifiers
- How to CultureInfo in C#
- How do I solve System.InvalidCastException?
- Difference between readonly and const keyword
- DateTime Format In C#
- Difference Between Task and Thread
- Object reference not set to an instance of an object
- How to Convert Char to String in C#
- How to Convert Char Array to String in C#
- How to convert int to string in C#?
Related Topics