Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
16
Question by Christophe F · May 05, 2010 at 08:38 AM · javascriptrunexecutable

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
25
Best Answer

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Christophe F · May 05, 2010 at 11:06 AM 1
Share

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.

avatar image ferraribeng · Nov 16, 2011 at 04:03 AM 4
Share

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.

avatar image ActionScripter · Jun 28, 2012 at 02:42 PM 0
Share

Would you believe it - this is still helping people. $$anonymous$$yself included. Thanks!

avatar image RodrigoSeVeN · Aug 05, 2012 at 07:28 PM 0
Share

This is even helping to help, the community thank you!

avatar image
14
Wiki

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);        
         }
     }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pranavgadamsetty · Feb 16, 2017 at 04:31 AM 0
Share

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.

solve.txt (493 B)
avatar image Yodzilla · Jul 11, 2018 at 02:54 PM 0
Share

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.

avatar image
2

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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");
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Structed · Jul 23, 2014 at 11:54 AM 3
Share

Erm, you mean JavaScript ;-)

avatar image urhire@yahoo.com · Oct 31, 2014 at 09:25 AM 0
Share

Didn't work!

avatar image LtKelleyUSMC · Oct 24, 2015 at 06:00 PM 0
Share

Actually, this should probably be the following:

 function Start()
 {
     System.Diagnostics.Process.Start("Notepad F:/temp/data.txt");
 }
avatar image esoinila · Aug 21, 2017 at 12:51 PM 0
Share

This method seems to work in editor but not in a PC-build. There it throws "Win32Exception: The system cannot find the file specified."

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Application.OpenURL problems with local folder 0 Answers

(Programming languages) Can Javascript/Unityscript be executed as standalone ? 1 Answer

mecanim run animation script trouble 0 Answers

Rotation accuracy in build 1 Answer

My Build won't run properly on Win 8.1 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges