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,353 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,711 people online right now. Registration is fast and FREE... Join Now!





Check if a process is running

This is a snippet I use often to check and see if a certain process is running. It loops through all the processes in your Task Manager looking for the name your provide it

Submitted By: PsychoCoder
Actions:
Rating:
Views: 23,130

Language: C#

Last Modified: July 27, 2009
Instructions: Pass the method a process name (minus the .EXE)

Snippet


  1. //Namespaces we need to use
  2. using System.Diagnostics;
  3.  
  4. public bool IsProcessOpen(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. Be sure to not
  11.                 //add the .exe to the name you provide, i.e: NOTEPAD,
  12.                 //not NOTEPAD.EXE or false is always returned even if
  13.                 //notepad is running.
  14.                 //Remember, if you have the process running more than once,
  15.                 //say IE open 4 times the loop thr way it is now will close all 4,
  16.                 //if you want it to just close the first one it finds
  17.                 //then add a return; after the Kill
  18.                 if (clsProcess.ProcessName.Contains(name))
  19.                 {
  20.                         //if the process is found to be running then we
  21.                         //return a true
  22.                         return true;
  23.                 }
  24.         }
  25.         //otherwise we return a false
  26.         return false;
  27. }

Copy & Paste


Comments


ankit_virgo239 2009-07-27 07:05:35

hi its giving error Foreach cannot operate on a 'method group'. Did you intend to invoke the 'method group'? and where to list the process v intend to check. Thanks


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