Kill some processes by .exe file name

白昼怎懂夜的黑 提交于 2019-11-26 07:25:42

问题


How can I kill some active processes by searching for their .exe filenames in C# .NET or C++?


回答1:


Quick Answer:

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

(leave off .exe from process name)




回答2:


My solution is:

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

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



回答3:


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




回答4:


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

Process processToKill = Process.GetProcessById(pid);
processToKill.Kill();



回答5:


You can Kill a specific instance of MS Word.

foreach (var process in Process.GetProcessesByName("WINWORD"))
{
    // Temp is a document which you need to kill.
    if (process.MainWindowTitle.Contains("Temp")) 
        process.Kill();
}



回答6:


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%.



来源:https://stackoverflow.com/questions/3345363/kill-some-processes-by-exe-file-name

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