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

时间:2015-11-08 02:58:52

标签: c# console-application

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

这是我正在使用的代码:

        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

我用它来读取输出流:

            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)

这没有意义,因为这是一个控制台应用程序? 我该如何解决这个问题? 或者有一个简单的工作?

编辑: 试过这个:ProcessInfo and RedirectStandardOutput

    static void Main(string[] args)
    {
        //C:\\Users\\pc\\Documents\\Visual Studio 2013\\Projects\\ConsoleApplication1\\ConsoleApplication1\\bin\\Debug\\VoliBot.exe
        Process build = new Process();
        build.StartInfo.WorkingDirectory = @"C:\\Users\\pc\\Documents\\Visual Studio 2013\\Projects\\ConsoleApplication1\\ConsoleApplication1\\bin\\Debug\\";
        build.StartInfo.Arguments = "";
        build.StartInfo.FileName = "volibot.exe";

        build.StartInfo.UseShellExecute = false;
        build.StartInfo.RedirectStandardOutput = true;
        build.StartInfo.RedirectStandardError = true;
        build.StartInfo.CreateNoWindow = true;
        build.ErrorDataReceived += build_ErrorDataReceived;
        build.OutputDataReceived += build_ErrorDataReceived;
        build.EnableRaisingEvents = true;
        build.Start();
        build.BeginOutputReadLine();
        build.BeginErrorReadLine();
        build.WaitForExit();

        Console.ReadLine();

    }

    static void build_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
        string strMessage = e.Data;
        Console.WriteLine(strMessage);
    } 

仍然无法工作:s

2 个答案:

答案 0 :(得分:1)

尝试类似:

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)
            {

            }
        }
    }

答案 1 :(得分:0)

你有@" C:\ Users \ pc \ Documents ...." 当你使用@符号时,你不需要加倍\

也许就是这样?

相关问题