0

我指的是这个问题: 在同一个控制台中启动一个进程

我在 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;

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

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

4

1 回答 1

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();
于 2014-07-04T15:56:19.993 回答