149

如何通过在 C# .NET 或 C++ 中搜索它们的 .exe 文件名来杀死一些活动进程?

4

8 回答 8

280

快速回答:

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

(从进程名称中去掉 .exe)

于 2010-07-27T15:48:34.463 回答
51

我的解决方案是Process.GetProcess()用于列出所有进程。

通过过滤它们以包含我想要的进程,然后我可以运行Process.Kill()方法来停止它们:

var chromeDriverProcesses = Process.GetProcesses().
    Where(pr => pr.ProcessName == "chromedriver"); // without '.exe'
    
foreach (var process in chromeDriverProcesses)
{
     process.Kill();
}

更新:

如果您想以异步方式(使用C# 8 Async Enumerables)执行相同操作,请查看:

const string processName = "chromedriver"; // without '.exe'
await Process.GetProcesses()
             .Where(pr => pr.ProcessName == processName)
             .ToAsyncEnumerable()
             .ForEachAsync(p => p.Kill());

注意:使用async方法并不总是意味着代码会运行得更快。
主要好处是前台线程将在运行时被释放。

于 2013-12-27T12:47:32.540 回答
18

您可以使用Process.GetProcesses()来获取当前正在运行的进程,然后Process.Kill()杀死一个进程。

于 2010-07-27T15:45:58.867 回答
1

如果您有进程 ID ( PID),您可以按如下方式终止此进程:

Process processToKill = Process.GetProcessById(pid);
processToKill.Kill();
于 2019-04-22T08:07:46.263 回答
1

您可以杀死 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();
}
于 2019-11-07T10:50:53.110 回答
0

根据要杀死的进程数量(例如,在我的情况下有数百个进程),遍历所有进程可能需要相当长的时间。(有趣的旁注:虽然 Kill() 在 .NET FW 4.8 中通常非常快,但在 NET 6.0 Windows 中它的速度要慢得多 - 在调试/跟踪中看到多个 Win32Exceptions,直到最终完成目标进程)

无论如何回到主题:在应用程序关闭的情况下,您需要确保每个进程都已消失,请考虑使用 TAP 库 - 特别是 Parallel 快捷方式,一目了然地杀死了数百个进程。

使用示例:

var procs = Process.GetProcessByName("mydirtyprocesses");

if (procs.Length == 0) return;

procs.AsParallel().ForAll(process => 
{
   try
   {
      process.Kill();

   // No process linked to the process comp (mostly because the process died in 
   // the short timespan between invoking GetProcess() and the effective 
   // initialization of the props/fields of the component. -OR- Process has 
   // already exited (when the exit happened after the process component has 
   // beenpopulated (difference is, in case 1 you cannot even get the Process 
   // ID from // the component, in case 2 you see data like Id and get the true 
   // for HasExited // - so always be prepared for that.
   // catch (InvalidOperationException) 
   {
     // Process is gone, no further action required
     return;
   }

   // Ensuring process is gone (otherwise try again or fail or whatever)
   if (!process.HasExited)
   { 
       // Handle it
   }
}

在这种特殊情况下,只需将其正确包装在 try/catch 中,因为有如此多的进程,异常的可能性会大大增加

于 2022-01-15T22:51:13.167 回答
0
static void Main()
    {
        string processName = Process.GetCurrentProcess().ProcessName;
        int processId = Process.GetCurrentProcess().Id;
        
        Process[] oProcesses = Process.GetProcessesByName(processName);

        if (oProcesses.Length > 1)
        {
            if ((MessageBox.Show("Application is opened!", "",MessageBoxButtons.YesNo) == DialogResult.Yes)) ;
            {
                foreach (var process in Process.GetProcessesByName(processName))
                {
                    if (process.Id != processId)
                    {
                        process.Kill();
                    }
                }
            }
        }
        else
        {   
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmLogin());
        }
    }
于 2022-02-27T16:02:48.587 回答
-1
public void EndTask(string taskname)
{
      string processName = taskname.Replace(".exe", "");

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

//EndTask("notepad");

摘要:无论名称中是否包含.exe,进程都会结束。您不需要“从进程名称中删除 .exe”,它可以 100% 工作。

于 2017-06-09T20:59:44.610 回答