Wednesday, August 05, 2009

Get Process Status using C#

The following code snippet can be used to get the status of any process or an application given its name or PID (Process ID).

using System.Diagnostics;
private void GetProcessStatus()
{
try
{
//If you know the name of the process
Process[] myProcess = Process.GetProcessesByName("notepad");
//If you know the PID of the process use the commented line below
//Process[] myProcess = Process.GetProcessById("1983");
//Check to see if the process array length is greater than 0
if(myProcess.Length > 0)
{
MessageBox.Show("The Process Notepad is currently running.", "Process Status", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("The Process Notepad is currently not running.", "Process Status", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch(Exception ex)
{
MessageBox.Show("An Exception Occoured: " + ex.Message, "Process Status", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

No comments:

Post a Comment