c#处理输出不可读的pckimmo

时间:2016-12-07 07:38:58

标签: c# process output

我一直在尝试阅读过程输出以解析形态分析。但我无法读取pckimmo32.exe输出。

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

1 个答案:

答案 0 :(得分: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;
}