- Home /
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 () {
}
}
Can't test it out at the moment... Just a thought though - have you tried doing it using P-Invoke? $$anonymous$$ight be interesting to see if you get better results.
Answer by thelackey3326 · Jan 31, 2012 at 12:48 AM
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.
I'm running into the same problem, however this answer does the same exact thing as GetProcessesByName().
Answer by miker88 · Oct 29, 2013 at 06:42 AM
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.
just verified in unity 5.2, running System.Diagnostics.Process.GetProcessesByName("Notepad"); in the editor works fine
Your answer
![](https://koobas.hobune.stream/wayback/20220613070947im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
In build - Process cannot find file 1 Answer
[SOLVED] Unable To Import Fonts? Unable to open assets/fonts check external settings 2 Answers
Integrate Unity inside a QT Application 0 Answers
How to launch another program when using or interacting with an object? 1 Answer
Embed and communicate with Unity inside another app 2 Answers