C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Become a C# Expert!

Join 300,308 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,077 people online right now. Registration is fast and FREE... Join Now!





Kill a process if its running

This is a snippet I use to search for and kill a process if it is found in the list of running processes.

Submitted By: PsychoCoder
Actions:
Rating:
Views: 25,728

Language: C#

Last Modified: February 19, 2009
Instructions: Pass the method the name of the process you want to kill (minus the .EXE)

Snippet


  1. //Namespaces needed
  2. using System.Diagnostics;
  3.  
  4. public bool FindAndKillProcess(string name)
  5. {
  6.         //here we're going to get a list of all running processes on
  7.         //the computer
  8.         foreach (Process clsProcess in Process.GetProcesses()) {
  9.                 //now we're going to see if any of the running processes
  10.                 //match the currently running processes by using the StartsWith Method,
  11.                 //this prevents us from incluing the .EXE for the process we're looking for.
  12.                 //. Be sure to not
  13.                 //add the .exe to the name you provide, i.e: NOTEPAD,
  14.                 //not NOTEPAD.EXE or false is always returned even if
  15.                 //notepad is running
  16.                 if (clsProcess.ProcessName.StartsWith(name))
  17.                 {
  18.                         //since we found the proccess we now need to use the
  19.                         //Kill Method to kill the process. Remember, if you have
  20.                         //the process running more than once, say IE open 4
  21.                         //times the loop thr way it is now will close all 4,
  22.                         //if you want it to just close the first one it finds
  23.                         //then add a return; after the Kill
  24.                         clsProcess.Kill();
  25.                         //process killed, return true
  26.                         return true;
  27.                 }
  28.         }
  29.         //process not found, return false
  30.         return false;
  31. }

Copy & Paste


Comments


Korupt 2008-08-15 16:22:26

"Foreach cannot operate on a 'method group'. Did you intend to invoke the 'method group'?" I get this compiler error

Dykam 2008-09-14 03:02:33

He need's to change foreach (Process clsProcess in Process.GetProcesses) { to foreach (Process clsProcess in Process.GetProcesses()) {

salahuddincse 2009-03-05 23:18:39

good

simontasker 2009-03-06 08:49:37

Hi, this is a great little snippet of code, im just curious how you would reference/call it in your main code? is the it similar to a function? Thanks

bshoemaker 2009-03-06 17:50:37

In addition, he also left out the fact that if the process is a system process, a Win32Exception will be hit, crashing the program. The clsProcess.Kill() code should be within a try/catch block catching Win32Exceptions.


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month