using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Diagnostics; namespace KillProcess { class Program { static void Main(string[] args) { Console.WriteLine("请输入进程名:"); string processName = Console.ReadLine(); int killValue = 0; killValue = KillProc(processName); //CloseProcess.KillProc(processName, ref killValue); switch (killValue) { case 0: Console.WriteLine("进程 {0} 关闭成功", processName); break; case 1: Console.WriteLine("当前没有正在运行的进程!"); break; case 2: Console.WriteLine("结束进程 {0} 出错!", processName); break; default: break; } Console.ReadKey(); } /// <summary> /// 关闭指定进程的方法 /// </summary> /// <param name="processName">进程名</param> /// <returns></returns> private static int KillProc(string processName) { int killValue = 0; try { Process[] ps = Process.GetProcesses(); if (ps == null) { killValue = 1; return killValue; } foreach (Process item in ps) { if (item.ProcessName.ToLower()==processName.ToLower()) { item.Kill(); } } } catch //出现异常,表明 kill 进程失败 { killValue = 2; } return killValue; } } }
时间: 2024-11-08 21:32:44