在控制台中启动进程

时间:2014-05-30 12:59:03

标签: c# console unity3d eclim

我指的是这个问题: Start a process in the same console

我在unity3d工作并尝试以下列方式从eclim接收项目列表:

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;

我也试过做以下事情:

 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;

最让我困惑的是,如果我手动输入控制台命令,我会得到正确的输出... Manual Console

我非常感谢任何合理的解释或解决方案。

1 个答案:

答案 0 :(得分:0)

更新

我终于能够通过以下方式解决问题:1#StartInfo的FileName属性丢失了,所以我添加了2#如果你想要执行你的参数字符串,你需要把一个/ C放入前面。

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