Ted
Ted

Reputation: 23

How to kill process by specific Id in c#

I have multiple processes that running at the same time of my C# program and all of this processes have the same name with different ID and I want to kill specific process of my program by their specific ID or rename them and kill it by their specific name.

Upvotes: 0

Views: 499

Answers (2)

rwpk9
rwpk9

Reputation: 334

If you have the id you can do it like this (where id is an int32):

Process process = Process.GetProcessById(id);
process.Kill();

Or if you are using LinQ you can do it like this:

Process.GetProcesses()
            .Where(x => x.Id.Equals(id))
            .FirstOrDefault()
            .Kill();

Upvotes: 0

Ergis
Ergis

Reputation: 1229


Edit:
Pay attention to Notes and Remarks sections of the documentation, specially for the Kill method.

Upvotes: 3

Related Questions