Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting a Process is already running in windows using C# .net

How do I detect if a process is already running under the Windows Task Manager? I'd like to get the memory and cpu usage as well.

like image 477
jinsungy Avatar asked Oct 09 '08 15:10

jinsungy


4 Answers

Simple example...

bool processIsRunning(string process)
{
    return (System.Diagnostics.Process.GetProcessesByName(process).Length != 0);
}

Oops... forgot the mem usage, etc...

bool processIsRunning(string process)
{
System.Diagnostics.Process[] processes = 
    System.Diagnostics.Process.GetProcessesByName(process);
foreach (System.Diagnostics.Process proc in processes)
{
    Console.WriteLine("Current physical memory : " + proc.WorkingSet64.ToString());
    Console.WriteLine("Total processor time : " + proc.TotalProcessorTime.ToString());
    Console.WriteLine("Virtual memory size : " + proc.VirtualMemorySize64.ToString());
}
return (processes.Length != 0);
}

(I'll leave the mechanics of getting the data out of the method to you - it's 17:15 here, and I'm ready to go home. :)

like image 135
ZombieSheep Avatar answered Oct 22 '22 10:10

ZombieSheep


Have you looked into the System.Diagnostics.Process Class.

like image 44
Ian Jacobs Avatar answered Oct 22 '22 10:10

Ian Jacobs


If you wanted to find out about the IE Processes that are running:

System.Diagnostics.Process[] ieProcs = Process.GetProcessesByName("IEXPLORE");

if (ieProcs.Length > 0)
{
   foreach (System.Diagnostics.Process p in ieProcs)
   {                        
      String virtualMem = p.VirtualMemorySize64.ToString();
      String physicalMem = p.WorkingSet64.ToString();
      String cpu = p.TotalProcessorTime.ToString();                      
   }
}
like image 27
Millhouse Avatar answered Oct 22 '22 10:10

Millhouse


You can use System.Diagnostics.Process Class.
There is a GetProcesses() and a GetProcessesByName() method that will get a list of all the existing processes in an array.

The Process object has all the information you need to detect if a process is running.

like image 35
StubbornMule Avatar answered Oct 22 '22 09:10

StubbornMule