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:	emp\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?

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

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\	estFile.bat";
		myProcess.StartInfo.Arguments = "/c" + path;
		myProcess.EnableRaisingEvents = true;
		myProcess.Start();
		myProcess.WaitForExit();
		int ExitCode = myProcess.ExitCode;
		//print(ExitCode);
		} catch (Exception e){
			print(e);		
		}
    }

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 () {
	
	}
}

I’m 8 years late, but here is a c# script I use daily that is incredibly simple. For the internet :slight_smile:

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.

here is a simple way in Java

function Start () {
System.Diagnostics.Process.Start("f:/temp/data.txt");
}