- Home /
Kill external process in Unity
I'm trying to open an external program in Unity and kill it afterwards by using this code:
Process myProcess;
string path;
path = Application.dataPath + "movie.avi";
myProcess = new Process();
myProcess.StartInfo.FileName = "C:\\Program Files\\Windows Media Player\\wmplayer.exe";
myProcess.StartInfo.Arguments = path;
myProcess.Start();
To kill it:
myProcess.CloseMainWindow();
Opening the file works fine, but I got the following error when Unity tries to kill it:
SystemException: No process to kill.
How to solve this? Thanks.
Also you shouldn't use an absolute hardcoded path. This wouldn't work on my pc because the "Program Files" folder is called different on a german installation ;) (or any other language)
You should be able to read the full media player application path from this registry key:
[H$$anonymous$$EY_LOCAL_$$anonymous$$ACHINE\SOFTWARE\$$anonymous$$icrosoft\$$anonymous$$ultimedia\W$$anonymous$$Player]
"Player.Path"
alternatively you could use Environment.GetFolderPath to get the actual "Program Files" path.
Anyway you should check if the media player is even installed / the exe file exists ;)
Answer by fanling3 · May 19, 2012 at 02:36 AM
Finally I got a solution:
foreach (Process p in Process.GetProcessByName("wmplayer")) {
p.CloseMainWindow();
}
I wanna kill the process in OnApplicationQuit, and this seems to be the only way it works! Thx.
Answer by Claytonious · May 18, 2012 at 08:56 AM
Try:
myProcess.Kill();
Instead of:
myProcess.CloseMainWindow();
Your answer
Follow this Question
Related Questions
System.Diagnostics.Process.StandardInput not defined. 1 Answer
Cross-platform way to start new process? 1 Answer
Detect how much RAM is available? (IL2CPP) Process.GetCurrentProcess().WorkingSet64 returns zero 0 Answers
How can I start a process to open Finder to point to a file? 1 Answer
Problem in starting mysqld Process 0 Answers