2016-12-07 58 views
0

我一直在尝试读取解析形态分析的过程输出。但我无法读取pckimmo32.exe输出。c#进程输出不可读pckimmo

public static string Problem1() 
{ 
    ProcessStartInfo _startInfo = new ProcessStartInfo(); 
    Process p = new Process(); 
    StringBuilder outputStringBuilder = new StringBuilder(); 
    string filePath = AppDomain.CurrentDomain.BaseDirectory + @"..\..\PC-KIMMO\pckimmo32.exe"; 
    var file = new FileInfo(filePath); 

    p.StartInfo = _startInfo; 
    _startInfo.UseShellExecute = false; 
    _startInfo.RedirectStandardOutput = true; 
    _startInfo.RedirectStandardInput = true; 
    _startInfo.WorkingDirectory = file.Directory.FullName; 
    _startInfo.FileName = file.FullName; 
    p.OutputDataReceived += (sender, eventArgs) => outputStringBuilder.AppendLine(eventArgs.Data); 
    p.Start(); 
    p.BeginOutputReadLine(); 
    var myWriter = p.StandardInput; 
    myWriter.AutoFlush = true; 
    myWriter.WriteLine("synthesize kitap +Noun +A3sg +P2sg +Loc"); 
    myWriter.Close(); 

    p.WaitForExit(); 
    var output = outputStringBuilder.ToString(); 

    return output; 
} 

public static void Display(DataReceivedEventArgs nes) 
{ 
    Console.WriteLine(nes.Data); 
} 

我可以读取另一个文本exe文件输出。

public static string Problem2() 
{ 
    ProcessStartInfo _startInfo = new ProcessStartInfo(); 
    Process p = new Process(); 
    StringBuilder outputStringBuilder = new StringBuilder(); 
    string filePath = AppDomain.CurrentDomain.BaseDirectory + @"..\..\..\RTest\bin\debug\RTest.exe"; 
    var file = new FileInfo(filePath); 

    p.StartInfo = _startInfo; 
    _startInfo.UseShellExecute = false; 
    _startInfo.RedirectStandardOutput = true; 
    _startInfo.RedirectStandardInput = true; 
    _startInfo.WorkingDirectory = file.Directory.FullName; 
    _startInfo.FileName = file.FullName; 
    p.OutputDataReceived += (sender, eventArgs) => outputStringBuilder.AppendLine(eventArgs.Data); 
    p.Start(); 
    p.BeginOutputReadLine(); 
    var myWriter = p.StandardInput; 
    myWriter.AutoFlush = true; 
    myWriter.Close(); 

    p.WaitForExit(); 
    var output = outputStringBuilder.ToString(); 

    return output; 
} 

Problem2方法是成功读取输出,我想读输出Problem1方法。

我相信我在正确的轨道上,但只需要几个指针。

Test project on the github

+0

pckimmo.exe一直使用错误输出输出。我可以用errorinput读取输出。 (RedirectStandardError,ErrorDataReceived,BeginErrorReadLine) –

回答

0

事情是这样的:

private string ReadProcessOutput(string fileName, TimeSpan waitTime, string args, string commandToEnter) // Command to enter in input window. 
{ 
    Console.WriteLine("Starting process: {0}", fileName); 

    Process proc = new Process 
    { 
     StartInfo = new ProcessStartInfo 
     { 
      FileName = fileName, 
      Arguments = args, 
      UseShellExecute = false, 
      RedirectStandardOutput = true, 
      RedirectStandardInput = true, 
      CreateNoWindow = true 
     } 
    }; 

    proc.Start(); 

    proc.StandardInput.WriteLine(commandToEnter); 
    proc.WaitForExit((int)waitTime.TotalMilliseconds); 

    if (proc.HasExited) 
    { 
     Console.WriteLine("Process {0} exited with code {1}", fileName, proc.ExitCode); 
     string output = proc.StandardOutput.ReadToEnd(); 
     Console.WriteLine("Process output: " + Environment.NewLine + output); 

     return output; 
    } 

    return null; 
} 
+0

我还没有读过pckimmo.exe输出。你可能会得到结果输出? –

+0

如果进程pckimmo.exe写入标准输出,这将以字符串形式返回。 –

+0

我不知道pckimmo内部进程,但是当文件运行时我看到命令输出。我的Problem2方法是读取标准输出响应。你看过github项目这个案例吗? –