我想将C++控制台的输出(printf(“”)语句)重定向到C#控制台。

以下是我所得到的

        Process e = new Process();
        e.StartInfo.UseShellExecute = false;
        e.StartInfo.RedirectStandardOutput = true;
        e.StartInfo.FileName = "C:\\Users\\Projects\\ot\\x64\\Debug\\ot.exe";
        e.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
        e.Start();
        e.BeginOutputReadLine();

但是我没有任何输出。 C++控制台不使用这些代码行(也不是C#)打印任何内容(这意味着输出已重定向)。

如何解决此问题?

谢谢,

最佳答案

尝试使用:

    Process e = new Process();
    e.StartInfo.UseShellExecute = false;
    e.StartInfo.RedirectStandardOutput = true;
    e.StartInfo.RedirectStandardError = true;
    e.StartInfo.FileName = "C:\\Users\\Projects\\ot\\x64\\Debug\\ot.exe";
    e.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
    e.ErrorDataReceived += (s, e) => Console.WriteLine(e.Data);
    e.Start();
    e.BeginOutputReadLine();
    e.BeginErrorReadLine();
    e.WaitForExit();
    e.CancelErrorRead();
    e.CancelOutputRead();

我整理了一个基类,可用于包装process class并帮助与其他进程进行交互。

关于c# - 将C++控制台输出重定向到C#控制台不会显示任何内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9141284/

10-13 01:49