A simple elaborated demo which will show you how to play with windows process in C#.
Here you find
Here you find
- ProcessName
- ProcessID
- How to Kill Process ID
- How to kill all Process by name.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using System.IO; namespace Process_ID_name_Kill_Csharp { class _Process { public static int Id() { Process currentProcess = Process.GetCurrentProcess(); return currentProcess.Id; } public static string Name() { Process currentProcess = Process.GetCurrentProcess(); return currentProcess.ProcessName; } public static bool Kill(int processId, ref string message) { bool _resp = false; try { if (processId > 0) { var p = Process.GetProcessById(processId); if (p != null) { p.Kill(); message = "Process Stopped."; } else { message = "Process Not Found."; } } else { message = "Invalid Process ID must be and positive integer."; } } catch(Exception exc) { message = "Error:" + exc.Message; } return _resp; } public static bool Kill(string processName, ref string message) { bool _resp = false; try { if (processName.Trim() != "") { var p = Process.GetProcessesByName(processName); if (p != null) { foreach (var item in p) { item.Kill(); } message = "Process Stopped."; } else { message = "Process Not Found."; } } else { message = "Invalid Process name must not be empty."; } } catch (Exception exc) { message = "Error:" + exc.Message; } return _resp; } public static ListAllByName(string processName) { var p = Process.GetProcessesByName(processName); return p.ToList(); } } class Program { static void Main(string[] args) { Console.WriteLine("Current process ID:" + _Process.Id()); Console.WriteLine("Current process:" + _Process.Name()); Console.WriteLine("process name count:" + _Process.AllByName(_Process.Name()).Count); string msgg = ""; //Kill all process by processname //_Process.Kill(_Process.Name(), ref msgg); //kill a process by processid. //_Process.Kill(_Process.Name(), ref msgg); Console.ReadKey(); } } }