Process.GetProcessesByName(string str); not working

Hi,

System.Diagnostics.Process.GetProcessesByName("notepad");

This line of code is not working in unity for me.
This works well in a non-unity mono project

I get the following error when ruining this line of code in unity 4.2.1f4:

InvalidOperationException: Process has exited, so the requested information is not available.
System.Diagnostics.Process.get_ProcessName ()
(wrapper remoting-invoke-with-check) System.Diagnostics.Process:get_ProcessName ()
System.Diagnostics.Process.GetProcessesByName (System.String processName)

I have no idea why its not working, any ideas?
Thank you.

Well, in Unity’s mono version GetProcessesByName simply calls GetProcesses and iterates over the whole list and compares the ProcessName. However there are processes which aren’t accessible and throw an exception when you try to read the ProcessName. So you should iterate through the array returned by GetProcesses yourself and catch exceptions for those processes which aren’t accessible.

This is fixed in later versions 4.6 at least. Just remember if the program is named explorer.exe you should pass “explorer” to the method and omit the exe extension.