Get process name using J#

For example if I want to check if alg.exe is running is there any simple way of doing that
Like if you can search for a process and return true or false if it is running or not.

Hope anyone can help me :)


Answer this question

Get process name using J#

  • DJ421

    Hi there,

    There are two ways, that I know of, that you can do this.

    First Way
    =======
    This way involves grabbing all the processes from your computer and iterating through all the running processes (the call to GetProcesses()) to find the one that you want. An example is below and it can easily be customised by replacing what process name it searches for.

    System.Diagnostics.Process[] processList = System.Diagnostics.Process.GetProcesses();

    String processNameAndExtension;

    boolean isProcessRunning = false;

    for (int i = 0; i < processList.length; i++)
    {
        processNameAndExtension = processList[ i].get_ProcessName() + ".exe"; //get_ProcessName() drops the ".exe"
        if (processNameAndExtension.IndexOf("alg.exe") >= 0)
        {
            isProcessRunning = true;
        }
    }

    if (isProcessRunning)
    {
        MessageBox.Show("alg.exe is running");
    }
    else
    {
        MessageBox.Show("alg.exe is not running");

    }

    Just one little thing, in the above code I have used [ i] to access an array element. This is incorrect as there should not be a space between the
    "[" and the "i" but I needed to do this for the purposes of the post because otherwise the Forum would render it as an emoticon. Sorry for the inconvenience.


    Second Way
    =========
    This way involves less code than the first way. It involves you knowing the name of the process, which is essentially the process executable without the ".exe" extension. As an example, if you wanted to check for "alg.exe" you would use "alg". As you know the name of the process being searched for, you can use the GetProcessByName() function.

    An example is below and, once again, it can be customised by replacing what process name is being searched for.

    System.Diagnostics.Process[] processList = System.Diagnostics.Process.GetProcessesByName("alg");


    if
    (processList.length > 0)

    {

        MessageBox.Show("alg.exe is running");

    }

    else

    {

        MessageBox.Show("alg.exe is not running");

    }


    Hope that helps a bit, but sorry if it doesn't


  • JohnBurns007

    Thanks NateV, That was exactly what I needed.

    Cheers.

  • Get process name using J#