标签:
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; } } }
标签:
原文地址:http://www.cnblogs.com/zy-style/p/4303236.html