C# 如何重定向控制台的输出?

C# 如何重定向控制台的输出?,c#,console-application,C#,Console Application,我目前正在尝试从外部控制台应用程序读取outputstream。 我无法修改此应用程序 这是我当前正在使用的代码: Process process = new Process(); string dir = Directory.GetCurrentDirectory(); process.StartInfo.FileName = dir + "/volibot.exe"; process.StartInfo.UseShellExec

我目前正在尝试从外部控制台应用程序读取outputstream。 我无法修改此应用程序

这是我当前正在使用的代码:

        Process process = new Process();
        string dir = Directory.GetCurrentDirectory();
        process.StartInfo.FileName = dir + "/volibot.exe";
        process.StartInfo.UseShellExecute = false;
        process.Start();
        //do some stuff with the stream
我使用它来读取outputstream:

            while (!process.StandardOutput.EndOfStream)
            {
                string message = process.StandardOutput.ReadLine();
                Console.WriteLine(message);
            }
但每当我补充:

        process.StartInfo.RedirectStandardOutput = true;
我得到这个错误:

Unhandled exception: System.IO.IOException: The handle is invalid.

   bij System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   bij System.IO.__Error.WinIOError()
   bij System.Console.SetWindowSize(Int32 width, Int32 height)
   bij RitoBot.Core.Main(String[] args)
这没有意义,因为这是一个控制台应用程序? 我如何解决这个问题? 还是有简单的解决办法

编辑: 我试过这个:

仍然不起作用:s

尝试以下方法:

var psi = new ProcessStartInfo(@"c:\temp\mycommand.exe", String.Format(" \"{0}\" \"{1}\" \"{2}\"", sourceDoc, dataSource, outputFile))
        {
            WorkingDirectory = Environment.CurrentDirectory,
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = false
        };

        using (var process = new Process { StartInfo = psi })
        {
            process.Start();
             process.BeginErrorReadLine();
              process.BeginOutputReadLine();

            // wait for the process to exit
            process.WaitForExit();


            if (process.ExitCode != 0)
            {

            }
        }
    }
您有@“C:\Users\pc\Documents…” 使用@符号时不需要双\号


可能是这样的?

您能显示读取
标准输出的代码吗?执行
volibot.exe
时是否有错误?或者到
volibot.exe
的路径不正确volibot从命令行运行正常。根据我在这里读到的内容:。也许你可以试试:
process.StartInfo.CreateNoWindow=true。不确定,虽然我猜
volibot.exe
中的代码使用了一些环境信息,例如,但是这些信息没有在代码中提供,导致volibot出错。当从命令行运行时,这些信息是由shell提供的。它给了我相同的崩溃:(不幸的是,不是这样的:(
var psi = new ProcessStartInfo(@"c:\temp\mycommand.exe", String.Format(" \"{0}\" \"{1}\" \"{2}\"", sourceDoc, dataSource, outputFile))
        {
            WorkingDirectory = Environment.CurrentDirectory,
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = false
        };

        using (var process = new Process { StartInfo = psi })
        {
            process.Start();
             process.BeginErrorReadLine();
              process.BeginOutputReadLine();

            // wait for the process to exit
            process.WaitForExit();


            if (process.ExitCode != 0)
            {

            }
        }
    }