Kill some processes by .exe file name

依然范特西╮ 提交于 2019-11-26 17:21:18
ConsultUtah

Quick Answer:

foreach (var process in Process.GetProcessesByName("whatever"))
{
    process.Kill();
}

(leave off .exe from process name)

My solution is:

var chromeDriverProcesses = Process.GetProcesses().
                                 Where(pr => pr.ProcessName == "chromedriver");

foreach (var process in chromeDriverProcesses)
{
     process.Kill();
}

You can use Process.GetProcesses() to get the currently running processes, then Process.Kill() to kill a process.

tomloprod

If you have the process ID (PID) you can kill this process as follow:

Process processToKill = Process.GetProcessById(pid);
processToKill.Kill();
user7993881
public void EndTask(string taskname)
{
      string processName = taskname.Replace(".exe", "");

      foreach (Process process in Process.GetProcessesByName(processName))
      {
          process.Kill();
      }
}

//EndTask("notepad");

Summary: no matter if the name contains .exe, the process will end. You don't need to "leave off .exe from process name", It works 100%.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!