如何杀死多个进程

时间:2013-05-20 09:48:23

标签: c# sql process task

我需要一些帮助,在我创建的程序中!我有一个简单的任务管理器,我可以杀死一个进程!但是,我需要它,所以我可以杀死多个进程,卡住了几天,有点不确定如何杀死多个进程。任何人都可以给我任何建议吗?

通过多个进程,我不是说只是在firefox中加入更多进程,我的意思是从listview或sql加载多个进程?

这是我的代码到目前为止。我想也许有可能将进程保存到sql,然后将它们加载到fire fox所在的位置?

foreach (System.Diagnostics.Process pr in System.Diagnostics.Process.GetProcesses())//GETS PROCESSES
{
  if (pr.ProcessName == "firefox")//KILLS FIREFOX.....REMOVE FIREFOX.....CONNECT SAVED SQL PROCESSES IN HERE MAYBE??
  {
      pr.Kill(); //KILLS THE PROCESSES
  }
}

1 个答案:

答案 0 :(得分:3)

DataSet ds = new DataSet();// SQL STUFF
SqlConnection con = new SqlConnection(ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("SELECT ProcessName FROM ApplicationProcesses", con);
// since you start from SqlDataAdapter I'm continue from there..           
da.Fill(ds, "ProcessNames");
// get the process in the database to a array
string[] preocesArray = ds.Tables["ProcessNames"]
        .AsEnumerable()
        .Select(row => row.Field<string>("ProcessName"))
        .ToArray();

// kill the process if it is in the list 
var runningProceses = System.Diagnostics.Process.GetProcesses();
for (int i = 0; i < runningProceses.Length; i++)
{
    if (preocesArray.Contains(runningProceses[i].ProcessName))
    {
        runningProceses[i].Kill(); //KILLS THE PROCESSES
    }
}