Kill some processes by .exe file name

214,255

Solution 1

Quick Answer:

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

(leave off .exe from process name)

Solution 2

My solution is to use Process.GetProcess() for listing all the processes.

By filtering them to contain the processes I want, I can then run Process.Kill() method to stop them:

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

Update:

In case if you want to do the same in an asynchronous way (using the C# 8 Async Enumerables), check this out:

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

Note: using async methods doesn't always mean code will run faster.
The main benefit is that the foreground thread will be released while operating.

Solution 3

You can use Process.GetProcesses() to get the currently running processes, then Process.Kill() to kill a process.

Solution 4

You can Kill a specific instance of 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();
}

Solution 5

If you have the process ID (PID) you can kill this process as follow:

Process processToKill = Process.GetProcessById(pid);
processToKill.Kill();
Share:
214,255

Related videos on Youtube

Aliasghar Yaghoobzadeh
Author by

Aliasghar Yaghoobzadeh

Updated on February 28, 2022

Comments

  • Aliasghar Yaghoobzadeh
    Aliasghar Yaghoobzadeh about 2 years

    How can I kill some active processes by searching for their .exe filenames in C# .NET or C++?

  • ConsultUtah
    ConsultUtah almost 14 years
    Process.GetProcessesByName would simplify this.
  • Manish
    Manish over 10 years
    what should be do if above code return Exception (a 32 bit processes cannot access modules of a 64 bit process) ?
  • Manish
    Manish over 10 years
    what should be do if above code return Exception (a 32 bit processes cannot access modules of a 64 bit process) ?
  • Admin
    Admin almost 10 years
    Leave off ".exe". From MSDN: "The process name is a friendly name for the process, such as Outlook, that does not include the .exe extension or the path"
  • AgainMe
    AgainMe over 7 years
    Is Kill safe as Environment.Exit(0)?
  • jchitel
    jchitel about 7 years
    @AgainMe Process.Kill() sends a kill signal to a process, which will halt its execution wherever it happens to be. This is different from an interrupt signal in that the process will not have a chance to respond and/or clean up from the signal. No more execution will happen in that process, and any locks on resources used by that process will be released. Environment.Exit() is performed by the currently executing process to kill itself with a success code, which is perfectly safe. Process.Kill() is not nearly as safe as Environment.Exit().
  • Leandro Bardelli
    Leandro Bardelli almost 7 years
    I suggest the use of LINQ: var procs = Process.GetProcesses().Where(pr => pr.ProcessName.Contains("Spotify"));
  • Leandro Bardelli
    Leandro Bardelli almost 7 years
    you could use Contains instead of equal
  • kerl
    kerl over 6 years
    Funny coincidence is, I was looking in this thread for a solution to killing the chromedriver. Must be a common issue.
  • AndrewK
    AndrewK over 6 years
    a simple .Replace(".exe", "") on the top voted answer would do this with a lot less convoluted and unnecessary code
  • Banee Ishaque K
    Banee Ishaque K about 6 years
    Any option to kill a specific instance of a process? I mean, Contains("Spotify")) kills all the instances of Spotify. I want to kill a particular instance of Spotify.
  • user7993881
    user7993881 about 6 years
    The whole idea of it is to see the method with or without .exe so people can see multiple ways of handling it... It's not meant for copy and paste....
  • Dan Csharpster
    Dan Csharpster almost 5 years
    Same here. that doesn't seem to solve it though. The initial console that gets fired off is actually an instance of chrome.exe and I'm guessing you don't want to force close all of those unless its a build/test agent
  • Anto Varghese
    Anto Varghese over 4 years
    @BaneeIshaqueK I had a similar requirement, I had used MainWindowTitle to find the right process and used CloseMainWindow to close it. You can try it
  • Sidupac
    Sidupac about 3 years
    this won't work if the name actually contains ".exe" e.g. "my.exeption.exe" Alternatively you could check substring of last four characters.. although again that would fail for a name with "my.exe.exe". Just pointing it out.