Starting a process in console

1.7k views Asked by At

I am referring to this question: Start a process in the same console

I am working in unity3d and trying to receive the projects list from eclim in the following way:

public static Process shell()
{ 
var pr = new Process();
pr.StartInfo = new ProcessStartInfo("\"C:/Program Files/eclipse_kepler/eclipse/eclim\"", "--nailgun-port 9091 -command projects"){
        UseShellExecute = false,
        RedirectStandardOutput = true,
        RedirectStandardInput = true,
        RedirectStandardError = true    
    };
UnityEngine.Debug.Log("attempting start with: "+pr.StartInfo.FileName +" "+ pr.StartInfo.Arguments);
//unfortunately my program breaks after the following line with System.ComponentMode.Win32Exception
pr.Start();
//never reaches the next lines
var proutput = pr.StandardOutput.ReadToEnd();
var prerror=pr.StandardError.ReadToEnd();
UnityEngine.Debug.Log("eror was: "+prerror);
UnityEngine.Debug.Log("output is: "+proutput);
return pr;

I also tried doing the following thing:

 public static Process shell()
{ 
var pr = new Process();
pr.StartInfo = new ProcessStartInfo(@"c:\windows\system32\netstat.exe", "-n"){
        UseShellExecute = false,
        RedirectStandardOutput = true,
        RedirectStandardInput = true,
        RedirectStandardError = true    
    };
UnityEngine.Debug.Log("attempting start with: "+pr.StartInfo.FileName +" "+ pr.StartInfo.Arguments);
//No Exception here and 
pr.Start();
//output works just fine
var proutput = pr.StandardOutput.ReadToEnd();
UnityEngine.Debug.Log("output is: "+proutput);
pr.WaitForExit();
return pr;

What confuses me most, is the fact that if I type in the console command manually, I am getting the correct output... Manual Console

I would be very thankful for any reasonable explanation or solution.

1

There are 1 answers

0
Setup On BEST ANSWER

UPDATE

I finally was able to solve the problem in the following way: 1# the FileName attribute of StartInfo was missing, so I added it 2# if you want your arguments string to be executed, you need to put a /C in front of it.

var p= new System.Diagnostics.Process();
    p.StartInfo = new ProcessStartInfo(arguments)
    {
        FileName="cmd.exe",
        Arguments = "/C"+arguments,
        CreateNoWindow = true,
        UseShellExecute = false,
        RedirectStandardOutput = true,
        RedirectStandardInput = true,
        RedirectStandardError = true
    };
p.Start();