简体   繁体   English

如何使用linq轻松删除多个进程

[英]How to delete multiple processes using linq easily

I have lets say two processes: notepad and taskmgr open.我可以说两个进程:记事本和 taskmgr 打开。 I want to kill those two or few more processes how can i do this?我想杀死这两个或几个进程,我该怎么做? So far i came up with this by googling around:到目前为止,我通过谷歌搜索得出了这个结论:

Process.GetProcesses().Where(p => p.ProcessName == "notepad,taskmgr").ToList().ForEach(y => y.Kill());

not sure how to differentiate between two processes above?不确定如何区分上述两个过程?

That linq query looks to be only finding a process when its name is "notepad,taskmgr". 该linq查询似乎仅在名称为“ notepad,taskmgr”的情况下才找到进程。 To get both, use a query like this: 要获得两者,请使用如下查询:

Process.GetProcesses().Where(p => p.ProcessName == "notepad" || p.ProcessName == "taskmgr").ToList().ForEach(y => y.Kill());

If you're going to be adding more processes, you might want to do something like this: 如果要添加更多的进程,则可能需要执行以下操作:

var processNamesToKill = new List<string> { "notepad", "taskmgr", "someotherprocess" };
Process.GetProcesses().Where(p => processNamesToKill.Contains(p.ProcessName)).ToList().ForEach(y => y.Kill());

You can then add other processes to that list. 然后,您可以将其他进程添加到该列表。 As for figuring out what ones you've found, in your ForEach, you can access the name by y.ProcessName and use it in a conditional, where needed. 至于找出您发现的名称,您可以在ForEach中通过y.ProcessName访问该名称,并在需要的情况下有条件地使用它。

Edit: add .exe to those process names if needed. 编辑:如果需要,将.exe添加到那些进程名称中。

Create a list or array with your processes to be killed, then check the list for the name of the process 创建一个要杀死您的进程的列表或数组,然后检查列表中进程的名称

List<string> killme = new List<string> { "notepad", "taskmgr" }

Process.GetProcesses().Where(p => killme.Contains(p.ProcessName)).ToList().ForEach(y => y.Kill());

One Liner, which was in an answer that was deleted: 一个班轮,在答复中被删除:

Process.GetProcesses().Where(p => new[] {"notepad", "taskmgr"}.Contains(p.ProcessName)).ToList().ForEach(y => y.Kill());

Are you trying to do this? 您是否正在尝试这样做?

Process.GetProcesses().Where(p => p.ProcessName == "notepad" || p.ProcessName == "taskmgr")
    .ToList().ForEach(y => y.Kill());

What I would do, if I were you, would be to just do it 1 name at a time. 如果我是你,我会做的只是一次做一个名字。 It's a lot easier to understand for anyone reading the code. 对于任何阅读代码的人来说,它都容易理解。

class Program
{
    static void Main(string[] args)
    {
        killProcess("notepad.exe");
        killProcess("taskmgr");
    }

    public static void killProcess(string name)
    {
        foreach (Process process in Process.GetProcessesByName(name))
        {
            process.Kill();
        }
    }
}

if the rest of your linq works, that where should just be something like 如果您的其他linq正常工作,那应该是

... Where( p => p.ProcessName == "notepad" || p.ProcessName == "taskmgr" ) ...

otherwise, you might want to make a set of the process names to kill, like 否则,您可能需要设置一组要杀死的进程名称,例如

var set = new HashSet<string>() { "notepad.exe", "taskmgr.txt", "someotherthing.exe" };
... linq .. Where( p=> set.Contains( p.ProcessName ) )...

or other various join operations on the set. 或其他各种集合操作。

        var processesToKill = new [] {"notepad", "iexplorer"};
        var processes = Process.GetProcesses().Where(p => processesToKill.Contains(p.ProcessName)).ToList();
        processes.ForEach(p => p.Kill());

If you have a list of processes you want to kill, you can use Contains , as doing p => p.ProcessName == "notepad,taskmgr" won't work (No process will have the name "notepad,taskmgr", adding a comma has no special meaning) 如果您有要杀死的进程列表,则可以使用Contains ,如p => p.ProcessName == "notepad,taskmgr"将不起作用(任何进程的名称都不会为“ notepad,taskmgr”,添加逗号没有特殊含义)

So instead try something like 所以试试

var processesToKill = new Collection<string> { "notepad", "taskmgr" };

foreach (var process in Process.GetProcesses().Where(p => processesToKill.Contains(p)))
{
    process.Kill();
}

To kill all processes by name purely with LINQ, in a single line, without having an error thrown, you can use...要仅使用 LINQ 按名称杀死所有进程,在一行中不抛出错误,您可以使用...

Process.GetProcessesByName("WinStore.App").ToList().Where(p => p.HasExited == false).ToList().ForEach(s => s.Kill());

Kind of obtuse at first... but it will ensure the processes it is killing have actually ended.起初有点迟钝......但它会确保它正在杀死的进程实际上已经结束。 Sometimes you'll get an error if you try to kill a process that is not running but has not yet exited (if it's currently exiting and is in process purgatory ...)如果你试图杀死一个没有运行但还没有退出的进程,有时你会得到一个错误(如果它当前正在退出并且在进程炼狱中......)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM