【问题标题】:Starting a process in console在控制台中启动进程
【发布时间】:2014-05-30 12:59:02
【问题描述】:

我指的是这个问题: 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;

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

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

【问题讨论】:

  • 尝试从标准错误中读取。
  • @SLaks 我试过这样做,但在 pr.Start() 之后我没有得到任何回报。 pr.StandardError.ReadToEnd() 永远不会被调用。

标签: c# console unity3d eclim


【解决方案1】:

更新

我终于能够通过以下方式解决问题: 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();

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 2010-10-18
    • 2023-03-23
    • 1970-01-01
    • 2011-08-12
    • 2010-10-30
    • 1970-01-01
    • 2017-09-15
    相关资源
    最近更新 更多