RComley/Process

From ETC Public Wiki
Jump to: navigation, search

Execute an external exe from C#

using UnityEngine;
using System.Collections;
using System.Diagnostics;
 
public class Example : MonoBehaviour 
{
    void Awake()
    {
        Process.Start ("Chrome.exe", "http://www.google.com");    
    }
}


Execute an external exe from Javascript

#pragma strict

import System.Diagnostics;

var process : Process;

function Start () 
{
    process = new System.Diagnostics.Process();
    process.StartInfo.FileName = "Chrome.exe";
    process.StartInfo.Arguments = "http://google.com";
    process.Start();
}


Executing a bat file from C#

    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);        
            }
        }
    }