xeLin xel
xeLin xel

Reputation: 49

Kill process by part of name

Can please some one tell me how to kill a process by part of name? Example: I want to kill "explorer" but in code I want to implant to kill it by word "explor" and the rest should find out by code. Here is the code so far:

    Process[] localByName = Process.GetProcessesByName("explorer");
        foreach (Process p in localByName)
        {
            p.Kill();


        }

Thank you

Upvotes: 0

Views: 572

Answers (2)

I4V
I4V

Reputation: 35353

var localByName = Process.GetProcesses()
                         .Where(p => p.ProcessName.Contains("explor"));
foreach (Process p in localByName)
{
    p.Kill();
}

Upvotes: 5

Reed Copsey
Reed Copsey

Reputation: 564403

You could get all of the processes, then search afterwards:

var processes = Process.GetProcesses();
foreach(var p in processes.Where(proc => proc.ProcessName.IndexOf(searchString, StringComparison.CurrentCultureIgnoreCase) > -1))
   p.Kill();

Upvotes: 5

Related Questions