private static void Main(string[] args) { try { if (args.Length > 0) { var regex = new Regex(@"^(-|/)" + args[0].Remove(0, 1)); if (regex.Match(args[0]).Value.ToUpper().Contains("TASKLIST")) { new TaskList(args); } else if (regex.Match(args[0]).Value.ToUpper().Contains("TASKKILL")) { new TaskKill(args); } else { MethodErrors.Error(new ArgumentException("invalid argument!")); } } else { MethodErrors.Error(new ArgumentException("invalid argument!")); } } catch (Exception e) { MethodErrors.Error(e); } }
private void GetProcessList(string argument) { try { var processes = Process.GetProcessesByName(argument); Rendering(processes); } catch (Exception e) { MethodErrors.Error(e); } }
private void GetProcessList() { try { var processes = Process.GetProcesses(); Rendering(processes); } catch (Exception e) { MethodErrors.Error(e); } }
private void KillProcess(string imageName) { try { var process = Process.GetProcessesByName(imageName); process[0].Kill(); } catch (Exception e) { MethodErrors.Error(e); } }
private void KillProcess(int pid) { try { var process = Process.GetProcessById(pid); process.Kill(); } catch (Exception e) { MethodErrors.Error(e); } }
public TaskKill(string[] args) { if (args[1].ToUpper() == "/PID" || int.TryParse(args[2], out _)) { int.TryParse(args[2], out var pid); KillProcess(pid); } else if (args[1].ToUpper() == "/IMAGENAME") { KillProcess(args[2]); } else { MethodErrors.Error(new ArgumentException("invalid argument!")); } }
private static void Rendering(Process[] processes) { try { Console.WriteLine("{0,-20}{1,14}{2,12}{3,13}", "Image Name", "PID", "Session#", "Mem Usage"); Console.WriteLine("{0,-20}{1,9}{2,12}{3,13}", "=========================", "========", "===========", "============"); foreach (var process in processes) { var processName = process.ProcessName.Length > 20 ? process.ProcessName.Remove(20) : process.ProcessName; Console.WriteLine("{0,-20}{1,14}{2,12}{3,13}", processName, process.Id, process.SessionId, process.PeakPagedMemorySize64); } } catch (Exception e) { MethodErrors.Error(e); } }
public TaskList(string[] args) { try { if (args.Length == 1) { GetProcessList(); } else if (args.Length == 2 || args[1].ToUpper() == "/F") { GetProcessList(args[2]); } else { MethodErrors.Error(new ArgumentException("invalid argument!")); } } catch (Exception e) { MethodErrors.Error(e); } }