通过.exe 文件名杀死一些进程

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

227090 次浏览

可以使用 Process.GetProcesses()获取当前运行的进程,然后使用 Process.Kill()终止进程。

快速回答:

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

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

我的解决方案是使用 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方法并不总是意味着代码会运行得更快。
主要的好处是前台线程将在操作时释放。

public void EndTask(string taskname)
{
string processName = taskname.Replace(".exe", "");


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


//EndTask("notepad");

摘要 : 无论名称是否包含。Exe,该过程将结束。你不需要“停止”。从进程名称执行 exe”,它工作100% 。

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

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

你可以杀死一个 MSWord 的特定实例。

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

取决于有多少个进程需要杀死(例如,当有数百个进程时,就像我的情况一样) ,对所有这些进程进行搜索可能需要相当长的时间。(有趣的旁注: 而 Kill ()通常在。NET FW 4.8,不知何故,在 NET 6.0 Windows 中要慢得多-在调试/跟踪中看到多个 Win32Exception,直到目标进程最终完成)

言归正传: 在应用程序关闭的情况下,您需要确保每个进程都消失了,可以考虑使用 TAP 库-特别是并行快捷方式,数百个进程在一瞬间被关闭。

用法例子:

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 中,因为有这么多进程,异常发生的可能性就会大大增加

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());
}
}