- Home /
Running an external EXE file from Unity
Dear community,
I am willing to run an external EXE file with command arguments. The Scripting API didn't provide me with information regarding System.IO or running external files. The best info received was a link to MS Framework related classes, but I am actually looking for a straightforward example.
I have searched UnityAnswers for some time about this, and the closest example I have found on UnityAnswers is this:
String path = @"f:\temp\data.txt";
Process foo = new Process();
foo.StartInfo.FileName = "Notepad.exe";
foo.StartInfo.Arguments = path;
foo.Start();
I do not use C# for my project but use Javascript instead. Process isn't recognized there.
Please, can you supply me with an example of how to run an external program like "../VideoPlayer/myVideoPlayer.exe -fullscreen" for instance?
Answer by Mike 3 · May 05, 2010 at 10:52 AM
Process is in the System.Diagnostics namespace - you'll need to have import System.Diagnostics at the top of your script to be able to use it like you're doing
Alternatively (though less preferable), you could use System.Diagnostics.Process instead
Thanks a great lot. I feel quite ashamed by how simple my problem was ; thanks again for your help in revealing where was my mistake.
Here is the full code:
(At start of script)
import System.Diagnostics;
(where you want it to happen :)
var stringPath = "../"; var myProcess = new Process(); myProcess.StartInfo.FileName = "Notepad.exe"; myProcess.StartInfo.Arguments = stringPath; myProcess.Start();
Will update if other problems are met.
Hi Christopher,
There is nothing to be ashamed of. Rest assured that what you posted will be and has been helpful to others. I am one of those receipents.
Would you believe it - this is still helping people. $$anonymous$$yself included. Thanks!
This is even helping to help, the community thank you!
Answer by havik23 · Dec 10, 2013 at 06:25 PM
For what it's worth here's a code example of executing a .bat file (i.e an external command) outside of unity with a cscript:
using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System;
public class TestBatch : MonoBehaviour {
// Use this for initialization
void Start () {
try {
Process myProcess = new Process();
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "C:\\Windows\\system32\\cmd.exe";
string path = "C:\\Users\\Brian\\Desktop\\testFile.bat";
myProcess.StartInfo.Arguments = "/c" + path;
myProcess.EnableRaisingEvents = true;
myProcess.Start();
myProcess.WaitForExit();
int ExitCode = myProcess.ExitCode;
//print(ExitCode);
} catch (Exception e){
print(e);
}
}
Thanks for posting this. It helped me a lot. But when I try to build an application in Windows Store it gives me this error "Assets\Solve.cs(9,9): error CS0246: The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?)". I have attached the script belowlink text. Please help.
This worked perfectly for us! Thanks!
Yeah for some reason we were never able to launch an application directly from C# but hitting a batch file worked fine. However (and again for some reason) the batch file still didn't find the executable until we added it to the PATH system variable in environment variables for the built version. From the editor it worked without this but as soon as we made a build we needed to add the folder we were referencing to the PATH even with the full executable path in the batch file.
Answer by sed · Feb 18, 2015 at 11:11 PM
Actually, running a bat file is much simpler than @havik23 puts it (given you have the the_script.bat just next to your Assets folder in the file hierarchy)
using UnityEngine;
using System.Collections;
using System.Diagnostics;
public class RunBat : MonoBehaviour {
// Use this for initialization
void Start () {
Process foo = new Process();
foo.StartInfo.FileName = "the_script.bat";
foo.StartInfo.Arguments = "put your arguments here";
foo.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
foo.Start();
}
// Update is called once per frame
void Update () {
}
}
Answer by Gono · Sep 23, 2018 at 01:58 AM
I'm 8 years late, but here is a c# script I use daily that is incredibly simple. For the internet :)
using System.Diagnostics;
using System;
private static void RunFile()
{
Process.Start(Environment.CurrentDirectory + @"data.txt");
}
The file will be opened just like double clicking it, and therefore uses the defaults you have set in windows. For example, I use it to run an autohotkey script in my builds folder.
Process.Start(Environment.CurrentDirectory + @"\Builds\LaunchClients.ahk");
just runs my script no questions asked.
You need System.Diagnostics
to use Process.Start()
You need System
to use Environment.CurrentDirectory
. It just returns your project's base directory, and can be replaced with manually typing a directory instead. You should use @ before the string if doing so.
Then just add on the filename you want. I use @ before the string to interpret it literally, so no special characters can mess with it. It's not needed for data.txt but if you put a filepath such as "\Builds\LaunchClients.ahk" the slashes may mess with you.
Answer by vfxjex · Feb 20, 2014 at 08:35 AM
here is a simple way in Java
function Start () {
System.Diagnostics.Process.Start("f:/temp/data.txt");
}
Actually, this should probably be the following:
function Start()
{
System.Diagnostics.Process.Start("Notepad F:/temp/data.txt");
}
This method seems to work in editor but not in a PC-build. There it throws "Win32Exception: The system cannot find the file specified."