Determine if an external application is running (Windows Standalone)

Hello,

The function that you would normally use to determine if an external application is running is:

System.Diagnostics.Process.GetProcessesByName("ProgramName")

This compiles, but throws an exception. I am guessing that it is some issue with windows7 security. If you get all of the processes and look at each one individually you would see some processes, but other would throw exceptions if you try to list their names.

The same code works properly when compiled by visual studio c#.

This is a test harness that will show you what i am talking about. Notice that not all of the processes are listed. Some processes that are even launched by you under your own account will generate an execption:

    using UnityEngine;
using System.Collections;

public class test: MonoBehaviour {

    // Use this for initialization
    void Start () {
        try
        {
            foreach (System.Diagnostics.Process p 
                        in 
                    System.Diagnostics.Process.GetProcesses())
            {
                try
                {
                    print(p.ToString());
                }catch(System.Exception e)
                {
                    print("Mini exception when trying to list the name " + e);
                }
            }
        }catch(System.Exception e)
        {
            print("Exception caught "  + e);
        }
    }

    // Update is called once per frame
    void Update () {

    }
}

This question is old, but I ran into the same problem today while testing before an alpha deployment. GetProcessesByName() works just fine on Windows XP (my development platform), but throws an InvalidOperationException in Windows 7 Pro.

Then I found this: Mono Bugzilla: Bug 596779

And it was reportedly fixed here: Release Notes Mono 2.6.7

The reported bug is similar to the described problem, and may be related. At any rate, we found today that we can reproduce this issue using Unity v3.4 and v3.5 RC1. So, this may be fixed when Unity catches up to Mono 2.6.7 (if it hasn't already). The answer at the moment seems to be to iterate through the whole list of processes, but catch the InvalidOperationException inside the loop body, similar to the way lowbloodsugar 1 has done. Here's an example of killing all running instances of Notepad:

void KillRunningInstances()
{
    //NOTE: GetProcessByName() doesn't seem to work on Win7
    //Process[] running = Process.GetProcessesByName("notepad");
    Process[] running = Process.GetProcesses();
    foreach (Process process in running)
    {
        try
        {
            if (!process.HasExited && process.ProcessName == "notepad")
            {
                process.Kill();
                process.WaitForExit(1000);
            }
        }
        catch (System.InvalidOperationException)
        {
            //do nothing
            UnityEngine.Debug.Log("***** InvalidOperationException was caught!");
        }
    }
}

It would be a good idea to make sure that the process you are trying to find is not causing the exception to throw, otherwise it will be ignored.

The methods in System.Diagnostics.Process work only after you built your app and does not work while in Editor mode. I’ve tested this in Win8.1 using Unity Pro 4.2.